{"id":131,"date":"2008-08-09T02:39:50","date_gmt":"2008-08-09T06:39:50","guid":{"rendered":"http:\/\/blogs.n1zyy.com\/andrew\/?p=131"},"modified":"2008-08-09T02:39:50","modified_gmt":"2008-08-09T06:39:50","slug":"duck-typing","status":"publish","type":"post","link":"https:\/\/blogs.n1zyy.com\/andrew\/2008\/08\/09\/duck-typing\/","title":{"rendered":"Duck Typing"},"content":{"rendered":"<p>I&#8217;m in fairly creative mood tonight, as evidenced by one (now two) blog postings, the PHP coding I did earlier, and the upload of some 75 random pictures to Facebook. I guess that&#8217;s what happens when I avoid the work I really should be doing. (Blech, JavaScript.)<\/p>\n<p>Anyways, a few days ago a coworker mentioned something to me about duck typing, and I again decided to shirk my responsibilities and experiment. The basic idea is to stand typing on its head and have an imperative system rather than declarative &#8212; as they say, &#8220;if it walks like a duck, and quacks like a duck, it must be a duck.&#8221;<\/p>\n<p><!--more--><\/p>\n<p>So how does that translate to actual code? Well, normally a class exports all its interfaces in a static list defined when the class is written. But sometimes it&#8217;d be nice to ask for a class that has all the features necessary to implement a given interface <i>even if the class wasn&#8217;t declared as an implementor<\/i>.<\/p>\n<p>Since there&#8217;s no language support for this in PHP, <a href=\"http:\/\/pastebin.com\/f49b3eead\">my (proof-of-concept) solution<\/a> relies exclusively on the <a href=\"http:\/\/www.php.net\/reflection\">Reflection<\/a> library to validate method existence and signatures.<\/p>\n<p>Time for an example.<\/p>\n<pre style=\"border: 1px solid #000000;margin: 5px 0 5px 0;padding: 5px\"><code>class B\n{\n  public function woot($a)\n  {\n    echo 'haha';\n  }\n}\n\ninterface A\n{\n  public function woot($a);\n}\n\nvar_dump(Goose::isA('A', new B()));\n<\/code><\/pre>\n<p>Notice that B never declares that it&#8217;s going to implement the interface A. Yet <code>Goose::isA<\/code> will return TRUE, because, while it&#8217;s not declared as such, B does satisfy all the requirements of the interface.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m in fairly creative mood tonight, as evidenced by one (now two) blog postings, the PHP coding I did earlier, and the upload of some 75 random pictures to Facebook. I guess that&#8217;s what happens when I avoid the work I really should be doing. (Blech, JavaScript.) Anyways, a few days ago a coworker mentioned [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11,20,23],"tags":[],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-development","category-hacks","category-php","category-programming"],"_links":{"self":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":0,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/andrew\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}