Christian
Christian

Reputation: 28165

PHP Extending class makes children inherit same static property

I'd like to start by showing a test case:

class A {
    public static $instance=null;
    public function __construct(){
        self::$instance=$this;
    }
    public function className(){
        return get_class(self::$instance);
    }
}

class B extends A {
    public function className(){
        return get_class(self::$instance);
    }
}

// test code
$b=new B();
echo $b->className; // B
$a=new A();
echo $a->className; // A
echo $b->className; // A <- error: not B any more!

Notes

The issues lies in the fact that self::$instance is the same for all instances. How do I separate self::$instance for each class?

Edit: I've had this idea:

$GLOBALS['store']=array();
class A {
    public static $instance=null;
    public function __construct(){
        $GLOBALS['store'][__CLASS__]=$this;
    }
}

Upvotes: 2

Views: 934

Answers (3)

Aether
Aether

Reputation: 364

You can add a public static $instance=null; declaration in class B.

class A {
    public static $instance=null;
    public function __construct(){
        self::$instance=$this;
    }
    public function className(){
        return get_class(self::$instance);
    }
}

class B extends A {
    public static $instance=null;
    public function className(){
        return get_class(self::$instance);
    }
}

// test code
$b=new B();
echo $b->className(); // B
$a=new A();
echo $a->className(); // A
echo $b->className(); // Now returns B, as desired.

Upvotes: 1

giraff
giraff

Reputation: 4711

You could store an instance per class name:

class A { 
    public static function getInstance(){
        // Maybe use this function to implement the singleton pattern ...
        return self::$instance[get_called_class()];
    }

    public function className(){
        return get_class(self::getInstance());
    }   
}

Upvotes: 3

Oliver A.
Oliver A.

Reputation: 2900

You can not do this the clean way. That is one of the mayor drawbacks on stati propertys: you cannot overrride them.

But you wantet an sollution so.....here is the worarround: use __calllStatic

  <?php 
 class A {
    public static function __callstatic($name,$args)
    {
        if($name="getClass"){
                return 'A';
        }
    }
 }

 class  B extends  A{
 public static function __callstatic($name,$args)
    {
        if($name="getClass"){
                return 'B';
        }
    }
 }


echo  A::getClass();
echo  B::getClass();
?>

the output of this is "AB";

Upvotes: 1

Related Questions