{"id":15026433,"url":"https://github.com/jitesoft/php-container","last_synced_at":"2026-02-19T18:31:29.688Z","repository":{"id":49634978,"uuid":"115048411","full_name":"jitesoft/php-container","owner":"jitesoft","description":"PSR-11 Container with Dependency Injection.","archived":false,"fork":false,"pushed_at":"2022-08-15T15:38:00.000Z","size":129,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-25T03:45:47.788Z","etag":null,"topics":["composer-package","constructor-injection","container","dependency-injection","hacktoberfest","ioc","php","php7","php72","php73","psr-11"],"latest_commit_sha":null,"homepage":"","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/jitesoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-12-21T21:25:20.000Z","updated_at":"2022-08-15T15:36:59.000Z","dependencies_parsed_at":"2022-09-19T06:51:17.647Z","dependency_job_id":null,"html_url":"https://github.com/jitesoft/php-container","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/jitesoft/php-container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-container","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-container/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-container/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-container/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jitesoft","download_url":"https://codeload.github.com/jitesoft/php-container/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-container/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29627111,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T18:02:07.722Z","status":"ssl_error","status_checked_at":"2026-02-19T18:01:46.144Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["composer-package","constructor-injection","container","dependency-injection","hacktoberfest","ioc","php","php7","php72","php73","psr-11"],"created_at":"2024-09-24T20:04:27.824Z","updated_at":"2026-02-19T18:31:29.669Z","avatar_url":"https://github.com/jitesoft.png","language":"PHP","funding_links":["https://opencollective.com/jitesoft-open-source"],"categories":[],"sub_categories":[],"readme":"[![pipeline status](https://gitlab.com/jitesoft/open-source/php/container/badges/master/pipeline.svg)](https://gitlab.com/jitesoft/open-source/php/container/-/commits/master)\n[![coverage report](https://gitlab.com/jitesoft/open-source/php/container/badges/master/coverage.svg)](https://gitlab.com/jitesoft/open-source/php/container/commits/master)\n[![Back project](https://img.shields.io/badge/Open%20Collective-Tip%20the%20devs!-blue.svg)](https://opencollective.com/jitesoft-open-source)\n[![Maintainability](https://api.codeclimate.com/v1/badges/55642eb87b3868861d91/maintainability)](https://codeclimate.com/github/jitesoft/php-container/maintainability)\n\n# Container\n\n[IoC](https://en.wikipedia.org/wiki/Inversion_of_control) container with [constructor dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).\n\n## Bindings\n\nThe container binds a key to a value where the value can be any type or a class name.  \n\nIf it is a object, primitive or any type of instance, it will store the instance as a static object, \nthat is: the same object will be returned at each `get` call. While if a class name is passed the container\nwill try to create an instance of the class on each `get` call. If it can not create an instance, a NotFoundException \nor ContainerException will be thrown.\n\nThe binding can be done at creation by passing an associative array into the constructor, or by using the `set` \nmethod. To re-bind, the `rebind` method - accepting a key and a value - can be used.  \n\nThe container implements the [PSR-11](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md) Container interface.\nFurther, the container implements the `ArrayAccess` interface, enabling fetching by using the array index syntax as: `$container[Interface::class]`.  \n\n## Usage\n\nThe container implements the following interfaces:\n\n\n```php\ninterface ContainerInterface {\n  public function get($abstract): mixed;\n  public function has($abstract): bool;\n}\n\ninterface ArrayAccess {\n  public function offsetExists($offset);\n  public function offsetGet($offset);\n  public function offsetSet($offset, $value);\n  public function offsetUnset($offset);\n}\n```\n\nThe `ArrayAccess` interface allows for getting, setting and un-setting bindings through the array index syntax:\n\n```php\n$c['Abstract'] = 'Concrete';\necho $c['Abstract']; // 'Concrete'\nunset($c['Abstract'];\n```\n\nFurthermore, the class implements the following public methods:\n\n```php\nclass Container {\n  public function __construct(?array $bindings);\n  public function clear();\n  public function set(string $abstract, mixed $concrete, ?bool $singleton = false): bool;\n  public function rebind(string $abstract, mixed $concrete, ?bool $singleton = false): void;\n  public function unset(string $abstract): void;\n  \n  public function singleton(string $abstract, mixed $concrete): void;\n}\n```\n\nThe constructor of the class takes an optional bindings array. The array expected to be an associative array\ncontaining the abstract as key and concrete as value. If wanted, the concrete could be another associative array with\na `class` or `func` key containing the class or callable to resolve to/with and a `singleton` key with a boolean value.  \nIf the singleton key is true, the container will only ever create a single instance of the resolved value or only run \nthe resolve function once.\n\nExample:\n\n```php\n$container = new Container([\n\n  // With objects:\n  InterfaceA::class =\u003e $objectInheritingInterfaceA,\n  InterfaceB::class =\u003e [\n    'class' =\u003e $objectInheritingInterfaceB,\n    'singleton' =\u003e true\n  ],\n\n  // With classes:\n  InterfaceA::class =\u003e ClassA::class,\n  InterfaceB::class =\u003e [\n    'class' =\u003e ClassB::class,\n    'singleton' =\u003e true\n  ],\n\n  // Or with functions:\n  InterfaceA::class =\u003e static fn (InterfaceB $ib) =\u003e new ClassA($ib),\n  InterfaceB::class =\u003e [\n    'class' =\u003e static function(InterfaceC $c) {\n      return new ClassB($c);\n    },\n    'singleton' =\u003e true\n  ],\n\n]);\n\n$container-\u003eget(InterfaceA::class); // Will be a new object of the ClassA class.\n$container-\u003eget(InterfaceB::class); // On first call, it will be resolved to a ClassB class.\n$container-\u003eget(InterfaceB::class); // On all other calls, the object will be the same as the first call.\n```\n\nAlternatively to the array type singleton binding, the interface will create a singleton binding with the `singleton` method.\n\nRebinding can be done in runtime with the `$container-\u003erebind($a, $c, $singleton);` method.  \nThis will unset the earlier binding and create a new.  \n\nTo remove all the current bindings, the `$container-\u003eclear();` method can be used, which will empty the inner\nlist of entries. Observe that this will not clear up the currently resolved instances of objects stored in your \nclasses, but rather just remove all the entries from the container.\n\nAll the methods implemented in the class (with an exception in the `has` and `clear` methods) will throw exceptions on errors.  \nThe following two exceptions are used:\n\n```\nJitesoft\\Exceptions\\Psr\\Container\\ContainerException implements ContainerExceptionInterface;\nJitesoft\\Exceptions\\Psr\\Container\\NotFoundException  implements NotFoundExceptionInterface;\n```\n\nSo when checking for exceptions, one could use either the underlying `JitesoftException` class, the specific classes or \nthe interfaces.  \nObserve though that the `NotFoundException` inherits from the `ContainerException` so in the cases where both can be returned and \nyou want to catch the specific exceptions, catch the `NotFoundException` before the `ContainerException`.\n\n### Dependency injection\n\nThe container will, in the cases where it is able to, inject dependencies into the constructor when resolving the object.  \nThere are some requirements before it will be able to do this though:  \n  \n1. The parameter need to be typehinted.\n2. The parameter need to be possible to resolve in the container or be possible to creat without constructor.\n\nIf the container can not resolve the parameter, it will throw an exception, but following the above two requirements, this should not happen.\n\n```php\nclass ClassA {}\n\nclass ClassB {\n  __constructor(ClassA $b) { }\n}\n\n$container-\u003eset(ClassB::class, ClassB::class);\n$container-\u003eget(ClassB::class); // Will throw a ContainerException due to class A not being bound.\n\n$container-\u003eset(ClassA::class, ClassA::class);\n$container-\u003eget(ClassB::class); // Will not throw any exception. ClassA is resolved and pushed into the constructor of ClassB.\n```\n\n### Not only classes\n\nThe container does not only resolve bindings, but can be used to store other values too.  \nIf the passed concrete is not a class name, it will use it as a single value and not resolve. So passing a string or number\nas the concrete will make the `get` method return the value.  \n\n```php\n$container-\u003eset('Something', 123);\n$container-\u003eget('Something'); // 123\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjitesoft%2Fphp-container","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjitesoft%2Fphp-container","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjitesoft%2Fphp-container/lists"}