diff --git a/_posts/06-02-01-Basic-Concept.md b/_posts/06-02-01-Basic-Concept.md index 965292d..070a7df 100644 --- a/_posts/06-02-01-Basic-Concept.md +++ b/_posts/06-02-01-Basic-Concept.md @@ -29,6 +29,7 @@ class MysqlAdapter {} {% endhighlight %} This code can be refactored to use Dependency Injection and therefore loosen the dependency. +Here, we inject the dependency in a constructor and use the [constructor property promotion][php-constructor-promotion] so it is available as a property across the class: {% highlight php %} adapter = $adapter; } } @@ -50,3 +48,5 @@ class MysqlAdapter {} Now we are giving the `Database` class its dependency rather than creating it itself. We could even create a method that would accept an argument of the dependency and set it that way, or if the `$adapter` property was `public` we could set it directly. + +[php-constructor-promotion]: https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion diff --git a/_posts/06-03-01-Complex-Problem.md b/_posts/06-03-01-Complex-Problem.md index 297ddf1..2fa18f4 100644 --- a/_posts/06-03-01-Complex-Problem.md +++ b/_posts/06-03-01-Complex-Problem.md @@ -82,11 +82,8 @@ namespace Database; class Database { - protected $adapter; - - public function __construct(AdapterInterface $adapter) + public function __construct(protected AdapterInterface $adapter) { - $this->adapter = $adapter; } } diff --git a/_posts/07-04-01-Interacting-via-Code.md b/_posts/07-04-01-Interacting-via-Code.md index 6721556..1ba2d58 100644 --- a/_posts/07-04-01-Interacting-via-Code.md +++ b/_posts/07-04-01-Interacting-via-Code.md @@ -70,11 +70,8 @@ include 'views/foo-list.php'; db = $db; } public function getAllFoos() {