{"id":21771154,"url":"https://github.com/initphp/container","last_synced_at":"2026-06-23T13:32:06.591Z","repository":{"id":41140918,"uuid":"485542873","full_name":"InitPHP/Container","owner":"InitPHP","description":"Simple Dependencies Container following PSR-11 standards","archived":false,"fork":false,"pushed_at":"2023-12-23T04:30:11.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-27T15:53:22.696Z","etag":null,"topics":["container","php","php7","psr-11","psr-container"],"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/InitPHP.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-25T21:38:32.000Z","updated_at":"2024-11-05T15:16:10.000Z","dependencies_parsed_at":"2024-11-26T18:18:29.562Z","dependency_job_id":null,"html_url":"https://github.com/InitPHP/Container","commit_stats":{"total_commits":4,"total_committers":2,"mean_commits":2.0,"dds":0.25,"last_synced_commit":"caa47c4bb339d8eefa434ae6be87dbce7d1f061a"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/InitPHP/Container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FContainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FContainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FContainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FContainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/InitPHP","download_url":"https://codeload.github.com/InitPHP/Container/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FContainer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273892837,"owners_count":25186561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"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","php","php7","psr-11","psr-container"],"created_at":"2024-11-26T14:15:14.081Z","updated_at":"2026-06-23T13:32:06.584Z","avatar_url":"https://github.com/InitPHP.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# InitPHP Container\n\nMinimal [PSR-11](https://www.php-fig.org/psr/psr-11/) dependency injection container with reflection-based autowiring.\n\n[![Latest Stable Version](https://poser.pugx.org/initphp/container/v/stable)](https://packagist.org/packages/initphp/container)\n[![Total Downloads](https://poser.pugx.org/initphp/container/downloads)](https://packagist.org/packages/initphp/container)\n[![License](https://poser.pugx.org/initphp/container/license)](https://packagist.org/packages/initphp/container)\n[![PHP Version Require](https://poser.pugx.org/initphp/container/require/php)](https://packagist.org/packages/initphp/container)\n\nThe container resolves entries lazily. You can register values, factories and class bindings explicitly, or let the container autowire a class straight from its name by reading its constructor signature through reflection. Every resolved entry is cached, so the container behaves as a shared (singleton) registry.\n\n## Requirements\n\n- PHP 8.1 or higher\n- [`psr/container`](https://packagist.org/packages/psr/container) `^2.0`\n\n## Installation\n\n```bash\ncomposer require initphp/container\n```\n\n## Quick Start\n\n```php\nrequire_once 'vendor/autoload.php';\n\nuse InitPHP\\Container\\Container;\n\nclass Mailer\n{\n}\n\nclass UserService\n{\n    public function __construct(public Mailer $mailer)\n    {\n    }\n}\n\n$container = new Container();\n\n// No registration needed: the container reads UserService's constructor,\n// builds the Mailer dependency automatically and injects it.\n$service = $container-\u003eget(UserService::class);\n\nvar_dump($service instanceof UserService); // bool(true)\nvar_dump($service-\u003emailer instanceof Mailer); // bool(true)\n```\n\n## Usage\n\n### Autowiring\n\nWhen you call `get()` with an existing class name, the container instantiates it and recursively resolves every class-typed constructor argument:\n\n```php\n$service = $container-\u003eget(UserService::class);\n```\n\nThe resolved instance is cached. Asking for the same identifier again returns the exact same object:\n\n```php\n$container-\u003eget(Mailer::class) === $container-\u003eget(Mailer::class); // true\n```\n\n### Storing values\n\n`set()` accepts any value. Scalars, arrays and objects are returned as they were stored:\n\n```php\n$container-\u003eset('app.name', 'InitPHP');\n$container-\u003eset('config', ['debug' =\u003e true]);\n$container-\u003eset('logger', new FileLogger('/var/log/app.log'));\n\n$container-\u003eget('app.name'); // 'InitPHP'\n$container-\u003eget('config');   // ['debug' =\u003e true]\n$container-\u003eget('logger');   // the same FileLogger instance\n```\n\n### Factories (closures)\n\nRegister a `Closure` to build an entry lazily. The closure receives the container and runs only once, the first time the entry is requested:\n\n```php\nuse Psr\\Container\\ContainerInterface;\n\n$container-\u003eset('pdo', function (ContainerInterface $c) {\n    return new PDO('sqlite::memory:');\n});\n\n$pdo = $container-\u003eget('pdo'); // closure runs here, result is cached\n```\n\n### Binding interfaces to implementations\n\nBind an interface (or any identifier) to a concrete class name so that both direct lookups and autowired dependencies resolve to the implementation:\n\n```php\ninterface LoggerInterface\n{\n}\n\nclass FileLogger implements LoggerInterface\n{\n}\n\nclass Report\n{\n    public function __construct(public LoggerInterface $logger)\n    {\n    }\n}\n\n$container-\u003eset(LoggerInterface::class, FileLogger::class);\n\n$container-\u003eget(LoggerInterface::class);      // FileLogger instance\n$container-\u003eget(Report::class)-\u003elogger;       // the same FileLogger instance\n```\n\n### Checking for an entry\n\n`has()` returns `true` when `get()` would not throw a `NotFoundException` — that is, the identifier is a registered entry or an existing class name:\n\n```php\n$container-\u003ehas('app.name');         // true after set()\n$container-\u003ehas(UserService::class); // true (autowirable class)\n$container-\u003ehas('missing');          // false\n```\n\n## How resolution works\n\n`get($id)` resolves in this order:\n\n1. Return the cached instance if `$id` was resolved before.\n2. If `$id` was registered with `set()`, build it (invoke the closure, instantiate the class name, or return the stored value) and cache it.\n3. If `$id` is an existing class name, autowire it and cache it.\n4. Otherwise throw `NotFoundException`.\n\nConstructor parameters are resolved as follows: a class-typed parameter is fetched from the container; otherwise its default value is used; otherwise `null` is supplied for nullable parameters. If none of these apply, resolution fails.\n\n## Exceptions\n\nAll exceptions live in `InitPHP\\Container\\Exception` and implement the relevant PSR-11 interface.\n\n| Exception | Implements | Thrown when |\n| --- | --- | --- |\n| `NotFoundException` | `Psr\\Container\\NotFoundExceptionInterface` | The identifier is neither registered nor an existing class. |\n| `DependencyIsNotInstantiableException` | `Psr\\Container\\ContainerExceptionInterface` | The target is an interface, abstract class, or has a non-public constructor. |\n| `DependencyHasNoDefaultValueException` | `Psr\\Container\\ContainerExceptionInterface` | A constructor parameter cannot be autowired and has no default or nullable fallback. |\n| `CircularDependencyException` | `Psr\\Container\\ContainerExceptionInterface` | A class depends on itself directly or through a cycle. |\n\n`ContainerException` is the base class for `NotFoundException` and the three resolution exceptions, so you can catch every container error with a single `catch (ContainerException $e)`.\n\n## Documentation\n\nIn-depth guides with runnable examples live in the [`docs/`](./docs) directory:\n\n- [Getting Started](./docs/getting-started.md)\n- [Autowiring](./docs/autowiring.md)\n- [Binding \u0026 Factories](./docs/binding-and-factories.md)\n- [Exceptions \u0026 Error Handling](./docs/exceptions.md)\n- [Limitations](./docs/limitations.md)\n\n## Testing\n\n```bash\ncomposer test      # PHPUnit\ncomposer stan      # PHPStan (max level)\ncomposer cs-check  # PHP-CS-Fixer (dry run)\n```\n\n## Contributing\n\nContributions are welcome. Please read the org-wide [CONTRIBUTING guide](https://github.com/InitPHP/.github/blob/main/CONTRIBUTING.md) before opening a pull request.\n\n## Credits\n\n- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) \u003c\u003cinfo@muhammetsafak.com.tr\u003e\u003e\n\n## License\n\nReleased under the [MIT License](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finitphp%2Fcontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finitphp%2Fcontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finitphp%2Fcontainer/lists"}