chazmeyers.com/blog


Broken static methods in PHP make me sad.

Posted in Programming, PHP by chazmeyers on the March 20th, 2007

A few weeks ago, I tried implementing ActiveRecord in PHP. What a fool I was. Consider the following:

Code (php)
  1.  
  2. <?php
  3.   class ActiveRecord {
  4.     public static $table = "NOTABLE";
  5.     static function selectAll() {
  6.       return "SELECT * FROM " . self::$table;
  7.     }
  8.   }
  9.  
  10.   class User extends ActiveRecord {
  11.     public static $table = "Users"
  12.   }
  13. ?>
  14.  

Sadly, User::findAll() returns “SELECT * FROM NOTABLE”.

I guess in order to implement ActiveRecord in PHP, you need to use some sort of combination of Singleton and Factory patterns? Gross.

Add this to the list of things that make me angry when I work in PHP. (The first item in that list is $a ||= “default” doesn’t work. Bah!)

It seems I’m not the only person who was saddened by this.