{"id":9334,"date":"2020-11-30T11:00:23","date_gmt":"2020-11-30T02:00:23","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9334"},"modified":"2020-11-27T19:47:19","modified_gmt":"2020-11-27T10:47:19","slug":"dependency-injection-in-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9334","title":{"rendered":"Dependency Injection in PHP"},"content":{"rendered":"\n<p>Dependency injection is the process by which one object provides the dependencies of another object. Dependency injection is a software design approach that avoids hard-coded dependencies and allows you to change dependencies at runtime and compile time.<\/p>\n\n\n\n<p>Lets take a look along with the examples. Lets say we have a Food class that will supply dependency to Dinner Class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Food\n{\n    protected $food;\n    \n    public function __construct($food)\n    {\n    \t$this->food = $food;\n    }\n    \n    public function get()\n    {\n    \treturn $this->food;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>After that, lets create a dinner class without dependency injection first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass Dinner\n{   \n    public function eat()\n    {\n    \t$food = new Food(\"Chicken soup\");\n        return $food->get();\n    }\n}\n\n$dinner = new Dinner();\necho $dinner->eat(); \/\/output - Chicken soup<\/code><\/pre>\n\n\n\n<p>In the above style of Dinner class, we have hardcoded food class that is bad in coupling and not well dependency injected. Lets fix our dinner class using dependency injection.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php \n\nclass Dinner\n{  \n\tprotected $dinner;\n    \n    \/\/receiving the dependency object food.\n\tpublic function __construct(Food $food)\n    {\n    \t$this->dinner = $food;\n    }\n    \n    public function eat()\n    {\n        return $this->dinner->get();\n    }\n}\n\n$food = new Food(\"Chicken soup\");\n\/\/supplying the food object.\n$dinner = new Dinner($food);\necho $dinner->eat(); \/\/output - Chicken soup<\/code><\/pre>\n\n\n\n<p>Now, we have added a constructor to be injected the necessary food dependency object. The code is now loosely coupled and not hardcoded in dinner class anymore.<\/p>\n\n\n\n<p>By Yuuma.<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9334\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9334\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Dependency Injection in PHP\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9334\" data-text=\"Dependency Injection in PHP\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9334\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9334\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Dependency injection is the process by which one object provides the dependencies of another object. Dependenc [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9334"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9334"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9334\/revisions"}],"predecessor-version":[{"id":9337,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9334\/revisions\/9337"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}