Broken static methods in PHP make me sad.
A few weeks ago, I tried implementing ActiveRecord in PHP. What a fool I was. Consider the following:
Code (php)
-
-
<?php
-
class ActiveRecord {
-
return "SELECT * FROM " . self::$table;
-
}
-
}
-
-
class User extends ActiveRecord {
-
}
-
?>
-
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!)
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.