{"id":639,"date":"2010-02-02T11:09:36","date_gmt":"2010-02-02T11:09:36","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=639"},"modified":"2010-02-02T11:09:36","modified_gmt":"2010-02-02T11:09:36","slug":"polymorphism-2","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/02\/polymorphism-2\/","title":{"rendered":"Polymorphism"},"content":{"rendered":"<p>Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface functions, so that any derived class will have to these functions implement so if you call a function that is defined by the interface to be present, you know it will be implement in some forum.<\/p>\n<p>In php, there is a interface type, so we can define a basic Animal type to print is name, so that any animal that is derived from this interface will have to implement at least the print name function, here is the interface<\/p>\n<pre lang=\"php\">\r\ninterface Animal\r\n{\r\n  public function printName();\r\n}\r\n<\/pre>\n<p>it is very similar to a class structure apart from there is no functional code, just the function definition.  To then implement the interface Animal within a class, so that you will know that the printName() function will be implemented you use the &#8220;implements&#8221; keyword in the class definition as below.<\/p>\n<pre lang=\"php\">\r\nclass Cat implements Animal\r\n{....\r\n<\/pre>\n<p>and then the class Cat will have to define the printName function as<\/p>\n<pre lang=\"php\">\r\n  public function printName()\r\n  {\r\n     echo \"Cat class\\n\";\r\n  }\r\n<\/pre>\n<p>otherwise if you did not implement it there would be a error on the &#8220;compile time&#8221; as below.<\/p>\n<pre lang=\"bash\">\r\nFatal error: Class Cat contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Animal::printName)\r\n<\/pre>\n<p>here is a full code that will do a Cat and Dog class, then you can create a array of different derived Animals interfaces and then just call the printName and you know it will be present.<\/p>\n<pre lang=\"php\">\r\n<?php\r\ninterface Animal\r\n{\r\n  public function printName();\r\n}\r\n\r\nclass Cat implements Animal\r\n{\r\n      public function printName()\r\n      {\r\n\techo \"Cat class\\n\";\r\n      }\r\n};\r\n\r\nclass Dog implements Animal \r\n{\r\n      public function printName()\r\n      {\r\n\techo \"Dog class\\n\";\r\n      }\r\n};\r\n\r\n$animals = Array(\r\n    new Cat(), new Dog()\r\n    );\r\n    \r\nforeach ($animals as $a)\r\n{\r\n    $a->printName();\r\n}\r\n\r\n?>\r\n<\/pre>\n<p>and the output would be<\/p>\n<pre lang=\"bash\">\r\nCat class\r\nDog class\r\n<\/pre>\n<p>Polymorphism is great, because you just know that certain functions will be implemented.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface functions, so that any derived class will have to these functions implement so if you call a function that is defined by the interface to &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/02\/polymorphism-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Polymorphism<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[85],"class_list":["post-639","post","type-post","status-publish","format-standard","hentry","category-php","tag-interface"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=639"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":640,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/639\/revisions\/640"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}