Skip to Content
Mastering PHP 7
book

Mastering PHP 7

by Branko Ajzele
June 2017
Intermediate to advanced
536 pages
9h 49m
English
Packt Publishing
Content preview from Mastering PHP 7

Understanding dependency injection

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:

<?phpclass 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(
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning PHP 7

Learning PHP 7

Antonio L Zapata (GBP)
Upgrading to PHP 5

Upgrading to PHP 5

Adam Trachtenberg

Publisher Resources

ISBN: 9781785882814Supplemental Content