{"id":24402286,"url":"https://github.com/p810/ioc-container","last_synced_at":"2025-10-15T21:46:32.841Z","repository":{"id":57034876,"uuid":"186516075","full_name":"p810/ioc-container","owner":"p810","description":"A dependency injection container that can autowire your objects with PHP's Reflection API","archived":false,"fork":false,"pushed_at":"2019-05-14T15:00:20.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-03T02:24:14.646Z","etag":null,"topics":[],"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/p810.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":"2019-05-14T00:41:54.000Z","updated_at":"2019-05-14T15:00:10.000Z","dependencies_parsed_at":"2022-08-24T06:50:06.586Z","dependency_job_id":null,"html_url":"https://github.com/p810/ioc-container","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/p810/ioc-container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p810%2Fioc-container","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p810%2Fioc-container/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p810%2Fioc-container/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p810%2Fioc-container/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/p810","download_url":"https://codeload.github.com/p810/ioc-container/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p810%2Fioc-container/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279120407,"owners_count":26108266,"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-10-15T02:00:07.814Z","response_time":56,"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":[],"created_at":"2025-01-20T00:59:51.983Z","updated_at":"2025-10-15T21:46:32.811Z","avatar_url":"https://github.com/p810.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ioc-container\n\u003e A dependency injection container that can autowire your objects with PHP's Reflection API\n\n## Getting started\nThis package is available via Packagist.\n\n```\n$ composer require p810/ioc-container --no-dev\n```\n\n`p810\\Container\\Resolver` describes any class that contains functionality to automatically resolve (autowire) classes in your codebase. `p810\\Container\\Container` is an abstract class that may be used as a base for resolvers, providing storage functionality for instances of `p810\\Container\\Entry`, which is a value object used to describe classes in the container.\n\nThe default resolver shipped with this package is `p810\\Container\\ReflectionContainer`, which uses the [Reflection extension](https://www.php.net/manual/en/intro.reflection.php).\n\n### Adding classes to the container\nYou can add classes to the container with `p810\\Container\\Container::set()`, providing a fully qualified class name. This will return an instance of `p810\\Container\\Entry` for your class:\n\n```php\n$container-\u003eset(Foo::class);\n```\n\nYou can specify a custom factory for any given class by passing a callable value as the second argument. By default, `p810\\Container\\ReflectionContainer::resolve()` is used. This method will pass your class's dependencies into the container for automatic resolution, a process known as autowiring.\n\n\u003e :bulb: **Note:** Any classes whose constructor contains parameters that are not objects must explicitly define those parameters via the `p810\\Container\\Entry` instance returned by the container. This is covered below, under [\"Specifying default values for parameters\"](#specifying-default-values-for-parameters).\n\n### Adding a singleton to the container\nIf you want an entry to only ever return one, specific object, either pass that object as the third argument to `p810\\Container\\Container::set()` or call `p810\\Container\\Container::singleton()`:\n\n```php\n$instance = new Foo(new Bar);\n\n// this:\n$container-\u003eset(Foo::class, $factory = null, $instance);\n\n// is the same as this:\n$container-\u003esingleton(Foo::class, $instance);\n```\n\nFor your class to be resolved by the container, supplying an instance may be skipped by omitting the second argument, or setting it to `null`.\n\nA callable may be passed as the third argument to use as the factory for instantiating your singleton. This is only used if you haven't already passed an instance.\n\nA fourth parameter, `$resolveNow`, is an optional boolean that will tell the container either to delay instantiation until the object is requested, or to do it immediately. This is set to `false` by default for delayed instantiation.\n\n```php\n// this will trigger a call to ReflectionContainer::resolve() when Foo is requested:\n$container-\u003esingleton(Foo::class);\n\n// this will invoke the given anonymous function when Foo is requested:\n$container-\u003esingleton(Foo::class, null, function () use ($bar): Foo {\n    return new Foo($bar);\n});\n\n// this will invoke the given callback immediately:\n$container-\u003esingleton(Foo::class, null, function (): Foo {\n    return new Foo(new Bar);\n}, true);\n```\n\n### Getting objects from the container\nA class can be resolved from the container by calling `p810\\Container\\Container::get()`. The `Resolver` will attempt to automatically resolve any type hinted classes it finds in the class's constructor, either in the method signature or as an `@param` annotation in its docblock.\n\n```php\nclass Foo {\n    /**\n     * @param Bar $bar\n     * @param Bam $bam\n     */\n    function __construct(Bar $bar, $bam) {\n        $this-\u003ebar = $bar;\n        $this-\u003ebam = $bam;\n    }\n}\n\n$foo = $container-\u003eget(Foo::class);\n```\n\nDefault arguments may be passed to `p810\\Container\\Container::get()` after the name of the class being resolved. If an associative array is the only given argument after the class name, it will be treated as a dictionary of named parameters; otherwise values will be looked up numerically.\n\n```php\n// this:\n$foo = $container-\u003eget(Foo::class, [\n    'bam' =\u003e new Bam,\n    'bar' =\u003e new Bar\n]);\n\n// is the same as this:\n$foo = $container-\u003eget(Foo::class, new Bar, new Bam);\n```\n\nArgument values may also be bound to the `p810\\Container\\Entry` instance for a given class.\n\n### Specifying default values for parameters\n`p810\\Container\\Entry::param()` allows you to bind values to parameters of your constructor by name:\n\n```php\nclass Bam {\n    function __construct(string $message) {\n        $this-\u003emessage = $message;\n    }\n}\n\n$entry = $container-\u003eset(Bam::class);\n\n$entry-\u003eparam('message', 'Hello world!');\n```\n\nYou can also use the plural counterpart `p810\\Container\\Entry::params()` to set multiple parameters with one call, by passing an associative array:\n\n```php\nclass Quux {\n    function __construct(string $greeting, string $subject) {\n        $this-\u003emessage = \\ucfirst($greeting) . ' ' . $subject . '!';\n    }\n}\n\n$entry = $container-\u003eset(Quux::class);\n\n$entry-\u003eparams([\n    'greeting' =\u003e 'hello',\n    'subject'  =\u003e 'world'\n]);\n```\n\n### Binding a specific class to an interface\nThe container can be configured to return an object of a specific class when a given interface is requested by means of `p810\\Container\\Container::bind()`. Give it the fully qualified class names of both the interface and its implementor:\n\n```php\ninterface Baz {\n    public function sayHello(string $subject): string;\n}\n\nclass Bem implements Baz {\n    public function sayHello(string $subject): string {\n        return \"Hello $subject!\";\n    }\n}\n\n$container-\u003ebind(Baz::class, Bem::class);\n\nvar_dump($container-\u003eget(Baz::class) instanceof Bem::class); //=\u003e bool: true\n```\n\n\u003e :bulb: **Note:** If the class you pass to `p810\\Container\\Container::bind()` has not already been registered to the container, it will be registered with the default settings. To customize a class's configuration you must register it before binding it to an interface.\n\n## License\nThis package is free and open source under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp810%2Fioc-container","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fp810%2Fioc-container","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp810%2Fioc-container/lists"}