{
    "href": "/post/2022/01/01/capsule-di-and-argument-inheritance/",
    "relId": "2022/01/01/capsule-di-and-argument-inheritance",
    "title": "Capsule DI and Argument Inheritance",
    "author": "pmjones",
    "tags": [
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/capsule/",
            "relId": "capsule",
            "title": "Capsule",
            "author": null,
            "created": "2021-09-13 21:01:20 UTC",
            "updated": [
                "2021-09-13 21:01:20 UTC"
            ],
            "markup": "markdown"
        }
    ],
    "created": "2022-01-01 22:16:20 UTC",
    "updated": [
        "2022-01-01 22:16:20 UTC",
        "2022-01-02 22:44:38 UTC"
    ],
    "markup": "markdown",
    "html": "<p>I am pleased to announce that I have released <a href=\"http://capsulephp.com\">Capsule</a> version 3.4.0, now with argument inheritance. (You can see the rest of the changes <a href=\"https://github.com/capsulephp/di/releases/tag/3.4.0\">here</a>.)</p>\n<p>As an autowiring dependency injection system for PHP 8.0+, Capsule supports injection of constructor arguments.  With the 3.4.0 release, each class defined in Capsule now \"inherits\" the constructor arguments of its parent. This means you can specify a constructor argument on a parent class, and the child class will use it (that is, unless you set an overriding value on the child class).</p>\n<p>Let's say you have this class hierarchy:</p>\n<pre><code class=\"language-php\">abstract class Strategy\n{\n    public function __construct(\n        protected string $path\n    ) {\n    }\n\n    abstract public function doSomething() : string;\n}\n\nclass FooStrategy extends Strategy\n{\n    public function doSomething() : string\n    {\n        // ...\n    }\n}\n\nclass BarStrategy extends Strategy\n{\n    public function doSomething() : string\n    {\n        // ...\n    }\n}\n</code></pre>\n<p>If you define the constructor arguments for the parent ...</p>\n<pre><code class=\"language-php\">$def-&gt;{Strategy::CLASS}\n    -&gt;argument('path', '/path/to/resources');\n</code></pre>\n<p>... both <code>FooStrategy</code> and <code>BarStrategy</code> will automatically \"inherit\" the\ndefined <code>$path</code> value. You won't have to re-define it for every <code>Strategy</code> class extension.</p>\n<p>You can always override the \"inherited\" values by specifying them for the child class directly ...</p>\n<pre><code class=\"language-php\">$def-&gt;{FooStrategy::CLASS}\n    -&gt;argument('path', '/path/to/other-resoruces');\n</code></pre>\n<p>... in which case the children of <em>that</em> class will inherit the override value. In this way, constructor arguments can be propagated down the inheritance hierarchy.</p>\n<p>If you decide that want to disable or interrupt the \"inheritance\" of arguments for a class, call <code>inherit(null)</code> on its definition:</p>\n<pre><code class=\"language-php\">$def-&gt;{BarStrategy::CLASS}\n    -&gt;inherit(null);\n</code></pre>\n"
}
