{"id":16673563,"url":"https://github.com/sasa-b/container","last_synced_at":"2026-05-22T15:44:53.887Z","repository":{"id":57047933,"uuid":"105656666","full_name":"sasa-b/container","owner":"sasa-b","description":"Lightweight dependency injection container with laravel like autowiring, interface/abstract class binding and contextual binding. PSR-11 compliant.","archived":false,"fork":false,"pushed_at":"2017-10-25T22:18:45.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-28T02:49:49.272Z","etag":null,"topics":["container","dependency","dependency-injection","dependency-injection-container","ioc","ioc-container","library","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sasa-b.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-03T13:43:23.000Z","updated_at":"2019-09-06T02:28:03.000Z","dependencies_parsed_at":"2022-08-23T19:00:18.709Z","dependency_job_id":null,"html_url":"https://github.com/sasa-b/container","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/sasa-b/container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasa-b%2Fcontainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasa-b%2Fcontainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasa-b%2Fcontainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasa-b%2Fcontainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sasa-b","download_url":"https://codeload.github.com/sasa-b/container/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sasa-b%2Fcontainer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33350667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-21T12:23:38.849Z","status":"online","status_checked_at":"2026-05-22T02:00:06.671Z","response_time":265,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["container","dependency","dependency-injection","dependency-injection-container","ioc","ioc-container","library","php"],"created_at":"2024-10-12T12:27:14.127Z","updated_at":"2026-05-22T15:44:53.854Z","avatar_url":"https://github.com/sasa-b.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# IOC container\nLightweight dependency injection container with laravel like autowiring, interface/abstract class binding and contextual binding. PSR-11 compliant.\n\n*************\n\nTo use the library clone the repo or just use composer to install it.\n\n`composer require sasa-b/container`\n\n## Usage examples\n\n```php\nrequire '../vendor/autoload.php';\n\n$container = new Foundation\\Container\\Container();`\n```\n\n#### Binding/Registering services\n\nA service (bound value) can be a string representation of a class, an anonymous function which returns an object instance, or an object instance itself. If you bind an object instance itself, that service will essentially act as a singleton because the same instance will always be returned.\n\n```php\n$container-\u003ebind(Foo\\Bar::class, Foo\\Bar::class); \n\n$container-\u003ebind(Foo\\Bar::class, function ($container) { \n   return new \\Foo\\Bar($container['Foo\\Baz']);\n   \n}); \n\n$container-\u003ebind('Foo\\Bar', FooBar::class); \n\n$container-\u003ebind(Foo\\Bar::class, new \\Foo\\Bar());\n```\n##### Singletons\n\n```php\n   $container-\u003ebind('Foo\\Bar', FooBar::class)-\u003eshare();\n   \n   //or\n   \n   $container-\u003ebind('Foo\\Bar', FooBar::class);\n   $container-\u003eshare(Foo\\Bar::class);\n   \n   $container-\u003ebind(Foo\\Baz::class, Foo\\Baz::class)-\u003emapTo('baz');\n   $container-\u003eshare('baz');\n```\n\n\n#### Mapping services to keys\nFor convenience you can map your services to keys and there are multiple ways of doing it.\n\n1. With mapTo method via method chaining\n\n```php\n$container-\u003ebind(Foo\\Bar::class, Foo\\Bar::class)-\u003emapTo('foo');\n```\n\n2. With the key method\n```php\n$container-\u003ekey('foo', Foo\\Bar::class);\n```\n\n3. Directly with the bind statement\n```php\n$container-\u003ebind('foo', function ($container) {\n   return new Foo\\Bar();\n});\n```\n\nWhen you directly bind services to a _key_, they will only be accessible by that _key_ and those services can't be used for **autowiring (injecting by type hinting)**, unless the service you are binding/registering is a string representaton of a class, in that case the service will automatically be mapped to the given _key_. \n\nIn case of `mapTo` and `key` bound/registered services are accessible by both the key and their class name, and because of that they can be used for autowiring. \n\n#### Binding/Registering multiple services\nFor registering multiple services at once you can use the `register` method or it's alias `bindMany`.\n\n```php\n$container-\u003eregister([\n    Foundation\\Request\\Http::class =\u003e Foundation\\Request\\Http::class,\n    Foundation\\Sessions\\SessionManager::class =\u003e \\Foundation\\Sessions\\SessionManager::class,\n    Foundation\\Request\\Cookie::class =\u003e Foundation\\Request\\Cookie::class,\n    'date' =\u003e \\DateTime::class,\n    Foundation\\Core\\Database::class =\u003e Foundation\\Core\\Database::class,\n    Foundation\\Database\\QueryBuilder::class =\u003e (function ($c) {\n        return new \\Foundation\\Database\\PDOQuery($c['db']);\n    })\n])-\u003emapTo(['request', 'session', 'cookie', 'db']);\n\n$container-\u003eregister([\n    Foundation\\Request\\Http::class =\u003e Foundation\\Request\\Http::class,\n    Foundation\\Sessions\\SessionManager::class =\u003e \\Foundation\\Sessions\\SessionManager::class,\n    Foundation\\Request\\Cookie::class =\u003e Foundation\\Request\\Cookie::class\n]);\n\n$container-\u003ekeys([\n   'request' =\u003e Foundation\\Request\\Http::class,\n   'session' =\u003e Foundation\\Sessions\\SessionManager::class,\n   'cookie' =\u003e Foundation\\Request\\Cookie::class\n]);\n```\n\n##### Registering multiple singletons\n\n```php\n/* Only those specified will be registered as singletons */\n$container-\u003eregister([\n    Foundation\\Request\\Http::class =\u003e Foundation\\Request\\Http::class,\n    Foundation\\Sessions\\SessionManager::class =\u003e \\Foundation\\Sessions\\SessionManager::class,\n    Foundation\\Request\\Cookie::class =\u003e Foundation\\Request\\Cookie::class,\n    'date' =\u003e \\DateTime::class,\n    Foundation\\Core\\Database::class =\u003e Foundation\\Core\\Database::class,\n    Foundation\\Database\\QueryBuilder::class =\u003e (function ($c) {\n        return new \\Foundation\\Database\\PDOQuery($c['db']);\n    })\n])-\u003eshare(['session', 'db', Foundation\\Core\\Database::class]);\n\n/* All will be registered as singletons */\n$container-\u003eregister([\n    Foundation\\Request\\Http::class =\u003e Foundation\\Request\\Http::class,\n    Foundation\\Sessions\\SessionManager::class =\u003e \\Foundation\\Sessions\\SessionManager::class,\n    Foundation\\Request\\Cookie::class =\u003e Foundation\\Request\\Cookie::class,\n    'date' =\u003e \\DateTime::class,\n    Foundation\\Core\\Database::class =\u003e Foundation\\Core\\Database::class,\n    Foundation\\Database\\QueryBuilder::class =\u003e (function ($c) {\n        return new \\Foundation\\Database\\PDOQuery($c['db']);\n    })\n])-\u003eshare();\n// or\n$container-\u003eregister([\n    Foundation\\Request\\Http::class =\u003e Foundation\\Request\\Http::class,\n    Foundation\\Sessions\\SessionManager::class =\u003e \\Foundation\\Sessions\\SessionManager::class,\n    Foundation\\Request\\Cookie::class =\u003e Foundation\\Request\\Cookie::class,\n    'date' =\u003e \\DateTime::class,\n    Foundation\\Core\\Database::class =\u003e Foundation\\Core\\Database::class,\n    Foundation\\Database\\QueryBuilder::class =\u003e (function ($c) {\n        return new \\Foundation\\Database\\PDOQuery($c['db']);\n    })\n], true);\n\n```\n\n#### Abstract binding\nYou can bind/register services to interfaces and abstract classes as well. These abstract bindings are convenient when used with autowiring, because you can type hint with abstractions (abstract classes and interfaces) and not concretions (implementations).\n\n```php\n$container-\u003ebind(Foo\\BarInterface::class, Foo\\Bar::class);\n$container-\u003ebind(Foo\\AbstractBaz::class, Foo\\Baz::class);\n```\n\n##### Contextual binding\nWhen you want to bind/register multiple different services to a same interface or abstract class you need to provide _context_ otherwise one will override the other. _Context_ is the third parameter to the `bind` method, and it can be a string representation of a class or a key mapped to a class, and that class needs to be the one in whose constructor or method the interface/abstract class is used as a typehint.\n\n```php\n// This will result in an override and `Foo\\Baz::class` will always be returned for Foo\\Bar::interface\n$container-\u003ebind(Foo\\BarInterface::class, Foo\\Bar::class);\n$container-\u003ebind(Foo\\BarInterface::class, Foo\\Baz::class);\n\n$container-\u003ebind(Foo\\BarInterface::class, Foo\\Bar::class);\n$container-\u003ebind(Foo\\BarInterface::class, Foo\\Baz::class, 'FooController');\n```\n\n#### Retrieveing services\n\n```php\n$service = $container['Foo\\Bar'];\n$service = $container[Foo\\Bar::class];\n$service = $container['foo'];\n\n// Only works for services mapped to keys\n$service = $container-\u003efoo();\n$service = $container-\u003efoo;\n\n// Retrieves a new instance or a singleton if a service has been registered as a singleton\n$service = $container-\u003eget('foo');\n\n// Always retrieves a singleton\n$service = $container-\u003eshared('foo');\n```\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsasa-b%2Fcontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsasa-b%2Fcontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsasa-b%2Fcontainer/lists"}