Pwnna
Pwnna

Reputation: 9538

PHP 5.3 Late static binding problem

<?php

class Base{
  protected static $somevar = false;
  public static function changeSomeVar(){
    static::$somevar = true;
  }

  public static function checkVar(){
    var_dump(static::$somevar);
  }
}

class Child1 extends Base{
  public static function setup(){
    static::changeSomeVar();
  }
}

class Child2 extends Base{

}

Child1::setup();
Child1::checkVar(); // true
Child2::checkVar(); // still true

?>

Is there a way to have Child1's $somevar different from Child2's $somevar?

(I know you could manually write protected static $somevar = false; in each subclass, but that's somewhat counter intuitive..)

Upvotes: 0

Views: 103

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 81988

If you want a child class to have a separate class level (static) variable, you will need to re-declare the variable. So you will need protected static $somevar = false; in the child classes.

When I think about class structures in other languages, it is very intuitive to require that.

Upvotes: 2

Related Questions