{"id":93,"date":"2009-02-13T11:34:19","date_gmt":"2009-02-13T16:34:19","guid":{"rendered":"http:\/\/mikeconley.ca\/blog\/?p=93"},"modified":"2023-12-20T16:25:22","modified_gmt":"2023-12-20T21:25:22","slug":"model-view-controller-in-php-model","status":"publish","type":"post","link":"https:\/\/mikeconley.ca\/blog\/2009\/02\/13\/model-view-controller-in-php-model\/","title":{"rendered":"Model-View-Controller in PHP:  Model"},"content":{"rendered":"<p>(Notes:<\/p>\n<ul>\n<li>the code here has been taken directly from the <a href=\"http:\/\/ca2.php.net\/oop5.late-static-bindings\" target=\"_self\" rel=\"noopener\">PHP Late Static Binding documentation<\/a><\/li>\n<li>if you want a more verbose and thorough explanation of this problem, <a href=\"http:\/\/www.actsasflinn.com\/trackbacks?article_id=php-and-activerecord&amp;day=08&amp;month=08&amp;year=2007 \" target=\"_self\" rel=\"noopener\">check this out<\/a><\/li>\n<\/ul>\n<p>)<\/p>\n<p>I have one thing to say for all of those PHP developers who look at Rails&#8217; ActiveRecord class and get all excited about implementing it in PHP:<\/p>\n<p>It can&#8217;t be done.\u00a0 Yet.<\/p>\n<p>Here&#8217;s why:<\/p>\n<p>Until PHP5.3, PHP does not implement a feature called &#8220;late static binding&#8221;.\u00a0 What is late static binding?\u00a0 Well, how about I show you what it&#8217;s like to NOT have late static binding:<\/p>\n<pre>class A {\r\n   public static function who() {\r\n     echo __CLASS__;\r\n   }\r\n   public static function test() {\r\n     self::who();\r\n  }\r\n}\r\nclass B extends A {\r\n  public static function who() {\r\n    echo __CLASS__;\r\n  }\r\n}\r\nB::test();  \/\/Outputs:  'A'<\/pre>\n<p>That&#8217;s right:\u00a0 B::test() outputs &#8216;A&#8217;.\u00a0 This is a problem, because while it is true that B is a subclass of A, B is still B.\u00a0 When I call a static method of B, I want it to know that it&#8217;s B.<\/p>\n<p>With late static binding (only available in PHp5.3 and onward), this goes away:<\/p>\n<pre>&lt;?php\r\nclass A {\r\n  public static function who() {\r\n    echo __CLASS__;\r\n  }\r\n  public static function test() {\r\n    static::who(); \/\/ Here comes Late Static Bindings\r\n  }\r\n}\r\nclass B extends A {\r\n  public static function who() {\r\n    echo __CLASS__;\r\n  }\r\n}\r\nB::test();  \/\/Outputs 'B'\r\n?&gt;<\/pre>\n<p>Why is this a problem for ActiveRecord?\u00a0 Well, say we define a class called ActiveRecord, and create a subclass of ActiveRecord called Person.\u00a0 When I call Person::find_all(), PHP5.2 is going to run find_all in ActiveRecord &#8211; and ActiveRecord&#8217;s find_all() will not know what kind of subclass I am.\u00a0 It won&#8217;t know if I&#8217;m a Person, Dog, Pizza, or any of that jazz.\u00a0 Essentially, ActiveRecord is now dead in the water.<\/p>\n<p>Now, you could just make find_all a standard method instead of a static one, but then for every find operation, you&#8217;d have to do this:<\/p>\n<pre>$p = new Person();\r\n$persons = $p-&gt;find_all();<\/pre>\n<p>Semantically, this doesn&#8217;t make much sense.<\/p>\n<p><em>But, PHP coders, take heart &#8211;\u00a0 there are two silver linings:<\/em><\/p>\n<ul>\n<li>PHP5.3 has late static bindings!\u00a0 As soon as it&#8217;s released, rejoice, and implement ActiveRecord<\/li>\n<li><a href=\"http:\/\/www.actsasflinn.com\/2007\/08\/08\/php-and-activerecord\" target=\"_self\" rel=\"noopener\">There&#8217;s always RowDataGateway!<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>(Notes: the code here has been taken directly from the PHP Late Static Binding documentation if you want a more verbose and thorough explanation of this problem, check this out ) I have one thing to say for all of those PHP developers who look at Rails&#8217; ActiveRecord class and get all excited about implementing [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,10],"tags":[40,14,39,38,11,12,1209,41],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-computer-science","category-php","tag-activerecord","tag-design-patterns","tag-late-static-binding","tag-model","tag-model-view-controller","tag-mvc","tag-php","tag-rowdatagateway"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/prmTy-1v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":21,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":3277,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/3277"}],"wp:attachment":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}