Strict Standards: Redefining already defined constructor for class wpdb in /home/cmeyers/chazmeyers.com/blog/wp-includes/wp-db.php on line 49

Deprecated: Assigning the return value of new by reference is deprecated in /home/cmeyers/chazmeyers.com/blog/wp-includes/cache.php on line 35

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /home/cmeyers/chazmeyers.com/blog/wp-includes/cache.php on line 400

Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 534

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 534

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 534

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 534

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 553

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 649

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 649

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 649

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 649

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el($output) in /home/cmeyers/chazmeyers.com/blog/wp-includes/classes.php on line 674

Deprecated: Assigning the return value of new by reference is deprecated in /home/cmeyers/chazmeyers.com/blog/wp-includes/query.php on line 15

Deprecated: Assigning the return value of new by reference is deprecated in /home/cmeyers/chazmeyers.com/blog/wp-includes/theme.php on line 505
chazmeyers.com/blog » Broken static methods in PHP make me sad.

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.

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