{"id":1302,"date":"2018-01-19T19:22:40","date_gmt":"2018-01-19T19:22:40","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-dependency-injection-creates-more-maintainable-code-page-2\/"},"modified":"2018-01-19T19:24:41","modified_gmt":"2018-01-19T19:24:41","slug":"php-dependency-injection-creates-more-maintainable-code-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-dependency-injection-creates-more-maintainable-code-page-2\/","title":{"rendered":"PHP Dependency Injection Creates More Maintainable Code Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By W. Jason Gilmore<\/div>\n<div class=\"\">on April 29, 2011<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Dependency Injection Containers<\/h2>\n<p>Although the previous example is a significant improvement, it still requires the implementer to know <em>something<\/em> about the instantiation requirements, including the fact that the <code>Account<\/code> class requires a construction parameter containing a database handler in the first place. Presuming accounts are managed throughout your application, this will require you to consistently instantiate the <code>mysql<\/code> class ahead of the <code>Account<\/code> class, thereby creating a higher-level dependency that although not embedded in the class, is nonetheless embedded throughout the application. Recognizing this drawback, you&#8217;ll commonly encounter the use of a <em>dependency container<\/em> in dependency injection implementations. A dependency container is merely an object that happens to know all about the dependency requirements of other objects, thereby allowing you to invoke the dependent object&#8217;s container rather than the dependent object itself. Consider this example:<\/p>\n<div class=\"example\"><code><\/p>\n<pre>class Container {\n\n  ... host, user, pswd, db, port pulled in via registry or similar\n\n  private function getDatabase() {\n    return new mysqli($this-&gt;_host, $this-&gt;_user, \n                      $this-&gt;_pswd, $this-&gt;_db, $this-&gt;_port);    \n  }\n  \n  public function createAccount() {\n    $account = new Account($this-&gt;getDatabase());\n  }\n\n}\n\n$container = new Container();\n\n$account = $container-&gt;createAccount();<\/pre>\n<p><\/code><\/div>\n<p>This approach is particularly useful when you need to resolve multiple class dependencies, because you&#8217;ll be able to encapsulate three, four, five, or more dependencies into the container, allowing you to simply call the <code>createAccount()<\/code> method.<\/p>\n<p>Because dependency management can quickly become a complex matter, a great deal of work has been put into general purpose dependency injection containers. Part of the Symfony project, the <a href=\"http:\/\/components.symfony-project.org\/dependency-injection\/\" target=\"_blank\">Symfony Dependency Injection<\/a> library is often cited as the gold standard within the PHP community, and is certainly worth a look if you plan on making DI part of your standard toolbox. I also suggest checking out the rather oddly-named <a href=\"https:\/\/github.com\/fabpot\/Pimple\" target=\"_blank\">Pimple<\/a>, a PHP 5.3-specific dependency injection container that also happens to be written by the Symfony project leader Fabien Potencier.<\/p>\n<h2>Further Reading<\/h2>\n<p>Dependency injection has been discussed at great length within many programming communities, with Martin Fowler offering a lengthy dissertation on the matter <a href=\"http:\/\/martinfowler.com\/articles\/injection.html\" target=\"_blank\">some years ago<\/a>. I also suggest learning more about DI solutions available within other languages, notably Java (see <a href=\"http:\/\/code.google.com\/p\/google-guice\/\" target=\"_blank\">Guice<\/a> and <a href=\"http:\/\/www.springsource.org\/\" target=\"_blank\">Spring<\/a> for two notable implementations).<\/p>\n<h2>About the Author<\/h2>\n<div class=\"articlePara\"><em><strong>Jason Gilmore<\/strong> is founder of the publishing, training, and consulting firm <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"newFrame\">WJGilmore.com<\/a>. He is the author of several popular books, including <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"newFrame\">&#8220;Easy PHP Websites with the Zend Framework&#8221;<\/a>, <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"newFrame\">&#8220;Easy PayPal with PHP&#8221;<\/a>, and <a href=\"http:\/\/www.amazon.com\/dp\/1430231149\/\" target=\"newFrame\">&#8220;Beginning PHP and MySQL, Fourth Edition&#8221;<\/a>. Follow him on Twitter at <a href=\"http:\/\/www.twitter.com\/wjgilmore\" target=\"newFrame\">@wjgilmore<\/a>.<\/em><\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Jason_Gilmore04292011.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Jason_Gilmore04292011.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Your PHP code is be more maintainable and when you implement dependency injection, decoupling otherwise dependent<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1302","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1302"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1302\/revisions"}],"predecessor-version":[{"id":3194,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1302\/revisions\/3194"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}