{"id":18576094,"url":"https://github.com/thiagodp/di","last_synced_at":"2025-04-10T08:31:02.340Z","repository":{"id":57040946,"uuid":"46059952","full_name":"thiagodp/di","owner":"thiagodp","description":"Dependency Injection for PHP","archived":false,"fork":false,"pushed_at":"2018-02-12T23:55:15.000Z","size":16,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T19:01:46.276Z","etag":null,"topics":["dependency-injection","injection","ioc","php","phputil"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thiagodp.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":"2015-11-12T14:56:45.000Z","updated_at":"2019-05-21T00:37:49.000Z","dependencies_parsed_at":"2022-08-23T22:00:08.800Z","dependency_job_id":null,"html_url":"https://github.com/thiagodp/di","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiagodp","download_url":"https://codeload.github.com/thiagodp/di/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248185320,"owners_count":21061494,"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","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":["dependency-injection","injection","ioc","php","phputil"],"created_at":"2024-11-06T23:23:33.491Z","updated_at":"2025-04-10T08:31:02.024Z","avatar_url":"https://github.com/thiagodp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# di\n\nDependency Injection for PHP.\n\n[![Build Status](https://travis-ci.org/thiagodp/di.svg?branch=master)](https://travis-ci.org/thiagodp/di)\n\nWe use [semantic version](http://semver.org/). See [our releases](https://github.com/thiagodp/di/releases).\n\n### Classes\n* [phputil\\DI](https://github.com/thiagodp/di/blob/master/lib/DI.php)\n \n### Installation\n```command\ncomposer require phputil/di\n```\n\n### Example 1: automatic injection without configuring anything\n\n```php\nclass A {}\nclass B {}\n\nclass C {\n\tpublic $a, $b;\n\tfunction __construct( A $a, B $b ) {\n\t\t$this-\u003ea = $a;\n\t\t$this-\u003eb = $b;\n\t}\n}\n\n// Automatically creates \"A\" and \"B\", and inject them in \"C\"\n$c = DI::create( 'C' );\n```\n\n### Example 2: configuring the injection for interfaces\n\n```php\ninterface MyInterface {\n\tfunction say( $what );\n}\n\nclass MyClass implements MyInterface {\n\tfunction say( $what ) { echo $what; }\n}\n\n// Configures \"MyInterface\" to be created using \"MyClass\"\nDI::config( DI::let( 'MyInterface' )-\u003ecreate( 'MyClass' ) );\n\n$foo = DI::create( 'MyClass' ); // Create an instance of MyClass (no configuration required)\n$foo-\u003esay( 'hello' );\n\n$bar = DI::create( 'MyInterface' ); // Create an instance of MyClass!\n$bar-\u003esay( 'world' );\n```\n\n### Example 3: a more complex model\n\n```php\ninterface I {}\n\nclass A {}\n\nclass B implements I {}\n\nclass C {\n\tpublic $i;\n\tfunction __construct( I $i ) {\n\t\t$this-\u003ei = $i;\n\t}\n}\n\nclass X {\n\tpublic $a, $c;\n\tfunction __construct( A $a, C $c ) {\n\t\t$this-\u003ea = $a;\n\t\t$this-\u003ec = $c;\n\t}\n}\n\n// Configures \"I\" to be created using \"B\"\nDI::config( DI::let( 'I' )-\u003ecreate( 'B' ) );\n\n// Automatically creates and injects \"A\" and \"C\", and\n// when creates \"C\", also injects \"B\" as \"I\".\n$x = DI::create( 'X' );\n```\n\n### Example 4: passing constructor's arguments\n\n```php\nclass A {\n\tfunction __construct( $one, $two = 'world' ) {\n\t\techo $one, ', ', $two;\n\t}\n}\n\n// Creates \"A\", passing constructor arguments as an array\n$a = DI::create( 'A', array( 'hello' ) ); // prints hello, world\n```\n\n### Example 5: configuring shared instances\n\n```php\nclass A {}\n\n// Makes \"A\" a shared instance\nDI::config( DI::let( 'A' )-\u003eshared() );\n\n$a1 = DI::create( 'A' );\n$a2 = DI::create( 'A' );\nvar_dump( $a1 === $a2 ); // bool(true)\n```\n\n### Example 6: configuring shared instances for interfaces\n\n```php\ninterface I {}\n\nclass C implements I {}\n\n// Makes \"I\" a shared instance\nDI::config( DI::let( 'I' )-\u003ecreate( 'C' )-\u003eshared() );\n\n$i1 = DI::create( 'I' );\n$i2 = DI::create( 'I' );\nvar_dump( $i1 === $i2 ); // bool(true)\n```\n\n### Example 7: defining a creation function\n\n```php\ninterface I {}\n\nclass C implements I {}\n\n// Let you using a callable to create the desired instance\nDI::config( DI::let( 'I' )-\u003ecall( function() {\n\t\treturn new C();\n\t} ) );\n\n$i = DI::create( 'I' ); // Calls your function to create a \"C\" instance\n```\n\n### Example 8: passing arguments for creation functions\n\n```php\nclass A {\n\tprivate $text;\n\tfunction __construct( $text ) {\n\t\t$this-\u003etext = $text;\n\t}\n}\n\n// Lets you customize a callable with parameters\nDI::config( DI::let( 'A' )-\u003ecall( function( $a, $b ) {\n\t\treturn new A( $a . $b );\n\t} ) );\n\n// Uses the customized constructor\n$a = DI::create( 'A', array( 'Hello, ', 'world' ) );\necho $a-\u003etext(); // Hello, world\n\n// Ignore the customized constructor passing true after the parameters\n$a2 = DI::create( 'A', array( 'Hi!' ), true );\necho $a2-\u003etext(); // Hi\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiagodp%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fdi/lists"}