Throughout the introductory section, we touched upon passing dependency through the class __construct() method. There is more to it than just passing the dependent object. Let's consider the following three seemingly similar but different examples.
Though PHP has been supporting type hinting for quite a while now, it isn't uncommon to come across pieces of code, which are as follows:
<?php
class App
{
protected $config;
protected $logger;
public function __construct($config, $logger)
{
$this->config = $config;
$this->logger = $logger;
}
public function run()
{
$this->config->setValue('executed_at', time());
$this->logger->log('executed');
}
}
class Config
{
protected $config = [];
public function setValue($path, $value)
{
// implementation...