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

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.

One Response to 'Broken static methods in PHP make me sad.'

Subscribe to comments with RSS or TrackBack to 'Broken static methods in PHP make me sad.'.

  1. David said,

    on January 30th, 2008 at 5:34 pm

    I’ve recently released a php library similar to the ActiveRecord pattern. I’ve encountered the same problems as yours. The actual implementation is neither using the singleton nor the factory pattern (well, to be honest, i have a factory utils class which is not published and not required, but which i use in my project). Instead, i use instance array variables to configure the db objects. The advantage is that each instance could overwrite its own configuration.

Leave a Reply