{"id":9359,"date":"2020-12-07T11:00:11","date_gmt":"2020-12-07T02:00:11","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9359"},"modified":"2020-12-07T19:51:45","modified_gmt":"2020-12-07T10:51:45","slug":"polymorphism-in-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9359","title":{"rendered":"Polymorphism in PHP"},"content":{"rendered":"\n<p>Polymorphism sounds difficult in terms of its vocabulary but it&#8217;s not that complex. Today I will write some explanations along with simple code examples. There are two types of polymorphism called dynamic polymorphism and static polymorphism.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the dynamic polymorphism first. It can be achieved using interface.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ninterface Country\n{\n    public function talk();\n}\n\nclass Myanmar implements Country\n{\n    public function talk()\n    {\n        return \"Mingalarpr\";\n    }\n}\n\nclass Japan implements Country\n{\n    public function talk()\n    {\n        return \"ko ni chi wa\";\n    }\n}\n?>\n<\/code><\/pre>\n\n\n\n<p>There is an interface called Country and two of the concrete classes called Myanmar and Japan implemented it. The type of two concrete classes, Myanmar &amp; Japan are same as they are the country but the function called <strong>talk<\/strong> inside their classes will be different depending on the country.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$myanmar = new Myanmar();\n$japan = new Japan();\n\necho $myanmar->talk(); \/\/output - Mingalarpr\necho $japan->talk(); \/\/output - ko ni chi wa<\/code><\/pre>\n\n\n\n<p>As I said earlier before their <strong>talk <\/strong>function is different. That is called dynamic polymorphism. Concrete classes are sharing the same interface as they are common in type but different in functionalities.<\/p>\n\n\n\n<p>There is one more polymorphism called static polymorphism. This can be achieved using method overloading. We will use <strong>__call <\/strong>magic method as we are writing in PHP.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nclass Talk\n{\n    function __call($name,$arg){\n        switch(count($arg)){\n            case 1 : return $arg[0];\n            case 2 : return $arg[0] . '&amp;' .$arg[1];\n        }\n    }\n}\n\n$talk = new Talk();\necho $talk->lang(\"Myanmar\"); \/\/output - Myanmar\necho $talk->lang(\"Myanmar\",\"Japan\"); \/\/output Myanmar &amp; Japan<\/code><\/pre>\n\n\n\n<p>There are different return depending on the method arguments. The class <strong>talk<\/strong> can be adaptable depending on the function arguments which is called static polymorphism.<\/p>\n\n\n\n<p>You can see the magic method __call reference here.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.overloading.php#object.call\">https:\/\/www.php.net\/manual\/en\/language.oop5.overloading.php#object.call<\/a><\/p>\n\n\n\n<p>Thanks for reading. 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\/9359\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9359\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Polymorphism 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\/9359\" data-text=\"Polymorphism 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\/9359\" 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\/9359\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism sounds difficult in terms of its vocabulary but it&#8217;s not that complex. Today I will write s [&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\/9359"}],"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=9359"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9359\/revisions"}],"predecessor-version":[{"id":9363,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9359\/revisions\/9363"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}