views:

30

answers:

1
<?php
class Base {
  protected static $c = 'base';

  public static function getC() {
    return self::$c;
  }
}

class Derived extends Base {
  protected static $c = 'derived';
}

echo Base::getC(); // output "base"
echo Derived::getC();    // output "base", but I need "derived" here!
?>

So what's the best workaround?

+1  A: 

The best way to solve this is to upgrade to PHP 5.3, where late static bindings are available. If that's not an option, you'll unfortunately have to redesign your class.

deceze
Is PHP 5.3 compatible with PHP 5.2? My server is still running on 5.2
powerboy
@powerboy It is **backwards** compatible, meaning scripts written for 5.2 will run on 5.3, for the largest part. Some minor things may have changed. See here: http://www.php.net/manual/en/migration53.php
deceze