{"id":20731088,"url":"https://github.com/greg-md/php-dependency-injection","last_synced_at":"2025-10-17T11:43:01.236Z","repository":{"id":62512571,"uuid":"95591536","full_name":"greg-md/php-dependency-injection","owner":"greg-md","description":"Dependency Injection technique for PHP.","archived":false,"fork":false,"pushed_at":"2019-07-23T22:13:18.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T10:21:49.898Z","etag":null,"topics":["dependency-injection","greg-dependency-injection","greg-md","greg-php","php","php-dependency-injection","web-artisans"],"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/greg-md.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-06-27T19:05:15.000Z","updated_at":"2019-07-23T22:13:20.000Z","dependencies_parsed_at":"2022-11-02T12:48:36.235Z","dependency_job_id":null,"html_url":"https://github.com/greg-md/php-dependency-injection","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/greg-md/php-dependency-injection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-dependency-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-dependency-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-dependency-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-dependency-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greg-md","download_url":"https://codeload.github.com/greg-md/php-dependency-injection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-dependency-injection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279335749,"owners_count":26150878,"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-17T02:00:07.504Z","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":["dependency-injection","greg-dependency-injection","greg-md","greg-php","php","php-dependency-injection","web-artisans"],"created_at":"2024-11-17T05:13:39.927Z","updated_at":"2025-10-17T11:43:01.205Z","avatar_url":"https://github.com/greg-md.png","language":"PHP","readme":"# Greg PHP Dependency Injection\n\n[![StyleCI](https://styleci.io/repos/95591536/shield?style=flat)](https://styleci.io/repos/29315729)\n[![Build Status](https://travis-ci.org/greg-md/php-dependency-injection.svg)](https://travis-ci.org/greg-md/php-dependency-injection)\n[![Total Downloads](https://poser.pugx.org/greg-md/php-dependency-injection/d/total.svg)](https://packagist.org/packages/greg-md/php-dependency-injection)\n[![Latest Stable Version](https://poser.pugx.org/greg-md/php-dependency-injection/v/stable.svg)](https://packagist.org/packages/greg-md/php-dependency-injection)\n[![Latest Unstable Version](https://poser.pugx.org/greg-md/php-dependency-injection/v/unstable.svg)](https://packagist.org/packages/greg-md/php-dependency-injection)\n[![License](https://poser.pugx.org/greg-md/php-dependency-injection/license.svg)](https://packagist.org/packages/greg-md/php-dependency-injection)\n\nDependency Injection provides a lightweight, but powerful IoC Container\nthat allows you to standardize and centralize the way objects are constructed in your application.\n\n# Table of Contents\n\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [How It Works](#how-it-works)\n    * [Inject](#inject)\n    * [Get](#get)\n    * [Expect](#expect)\n    * [Load](#load)\n    * [Call](#call)\n    * [Autoload](#autoload)\n* [License](#license)\n* [Huuuge Quote](#huuuge-quote)\n\n# Requirements\n\n* PHP Version `^7.1`\n\n# Installation\n\n`composer require greg-md/php-dependency-injection`\n\n# How It Works\n\nAll you need to start using the [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) technique,\nis to instantiate an IoC Container and inject objects in it.\n\n```php\n$ioc = new \\Greg\\DependencyInjection\\IoCContainer();\n```\n\n### Inject\n\n```php\n$ioc-\u003einject('foo', Foo::class);\n\n$ioc-\u003einject('bar', new Bar());\n```\n\n**You can also inject in a more elegant way, using the object name as abstract.**\n\n```php\n$ioc-\u003eregister(new Foo());\n```\n\nThe previous example is equivalent with:\n\n```php\n$ioc-\u003einject(Foo::class, new Foo());\n```\n\n**Customise the way your objects will be instantiated.**\n\n```php\n$ioc-\u003einject('redis.client', function() {\n    $redis = new \\Redis();\n\n    $redis-\u003econnect();\n\n    return $redis;\n});\n```\n\n### Get\n\nThe next example will return null if the object is not injected in the IoC Container.\n\n```php\n$foo = $ioc-\u003eget('foo');\n```\n\n### Expect\n\nThe next example will throw an exception if parameter is not injected in the IoC Container.\n\n```php\n$foo = $ioc-\u003eexpect('foo');\n```\n\n### Load\n\nIn a real application to take advantage of what's best from Dependency Injection technique,\nyou may want to instantiate objects with dependencies from the IoC Container without defining them manually.\nThe best way to do that is to inject objects with it's names or it's strategies names as abstracts.\n\nLet say we have the `Foo` class that requires a `BarStrategy` class.\n\n```php\nclass Foo\n{\n    private $bar;\n    \n    public function __construct(BarStrategy $bar)\n    {\n        $this-\u003ebar = $bar;\n    }\n}\n```\n\nWhat we do is inject the `BarStrategy` into the IoC Container and load the `Foo` class from it.\n\n\u003e `BarStrategy` is an `interface`,\n\u003e so, we don't break the [SOLID](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) principles.\n\n```php\n$ioc-\u003einject(BarStrategy::class, function() {\n    return new Bar();\n});\n\n$foo = $ioc-\u003eload(Foo::class);\n```\n\nSometimes you may want to redefine one or more dependencies of a class when loading it from the IoC Container.\n\n```php\nclass Foo\n{\n    private $bar;\n    \n    private $baz;\n    \n    public function __construct(BarStrategy $bar, BazStrategy $bar)\n    {\n        $this-\u003ebar = $bar;\n        \n        $this-\u003ebaz = $baz;\n    }\n}\n```\n\n```php\n$ioc-\u003einject(BarStrategy::class, function() {\n    return new Bar();\n});\n\n$ioc-\u003einject(BazStrategy::class, function() {\n    return new Baz();\n});\n```\n\nYou can easily do it by defining those dependencies next after the class name in `load` method.\n\n```php\n$customBaz = new CustomBaz();\n\n$foo = $ioc-\u003eload(Foo::class, $customBaz);\n```\n\nThe previous example will instantiate `BarStrategy` from the IoC Container, which is `Bar` class\nand for `BazStrategy` it will set the `CustomBaz` defined in the `load` method.\n\nYou can also load with arguments as array.\n\n```php\n$ioc-\u003eloadArgs(Foo::class, [new CustomBaz()]);\n```\n\n### Call\n\nYou can call a callable with arguments injected in the Ioc Container\nthe same way as [loading classes](#load).\n\n```php\n$ioc-\u003ecall(function(int $foo, Bar $bar) {\n    // $bar will be injected from the Ioc Container.\n}, 10);\n```\n\nYou can also call a callable using arguments as array.\n\n```php\n$ioc-\u003ecallArgs([$someObj, 'someMethod'], ...$arguments);\n```\n\n### Autoload\n\nYou can autoload some classes by defining their prefixes/suffixes as abstracts.\n\n```php\n$ioc-\u003eaddPrefixes('Foo\\\\');\n\n$ioc-\u003eaddSuffixes('Controller');\n\n$controller = $ioc-\u003eget(\\Foo\\BarController::class);\n```\n\n# License\n\nMIT © [Grigorii Duca](http://greg.md)\n\n# Huuuge Quote\n\n![I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. \u0026copy; #horrorsquad](http://greg.md/huuuge-quote-fb.jpg)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-md%2Fphp-dependency-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreg-md%2Fphp-dependency-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-md%2Fphp-dependency-injection/lists"}