我正在尝试实现这样的事情:
$child1_instance1 = new \aae\web\ChildOne();
$child1_instance2 = new \aae\web\ChildOne();
$child2_instance1 = new \aae\web\ChildTwo();
$child2_instance2 = new \aae\web\ChildTwo();
// setting the static variable for the first instance of each derived class
$child1_instance1->set('this should only be displayed by instances of Child 1');
$child2_instance1->set('this should only be displayed by instances of Child 2');
// echoing the static variable through the second instance of each drived class
echo $child1_instance2->get();
echo $child2_instance2->get();
//desired output:
this should only be displayed by instances of Child 1
this should only be displayed by instances of Child 2
// actual output:
this should only be displayed by instances of Child 2
this should only be displayed by instances of Child 2
具有与此类似的类结构:(我知道这不起作用。)
abstract class ParentClass {
protected static $_magical_static_propperty;
public function set($value) {
static::$_magical_static_propperty = $value;
}
public function get() {
return static::$_magical_static_propperty;
}
}
class ChildOne extends ParentClass { }
class ChildTwo extends ParentClass { }
如何为每个子类创建不跨兄弟共享的静态变量?
我希望能够这样做的原因是能够跟踪每个派生类应该只发生一次的事件,而不必让从我的父类派生的客户端担心所说的实现跟踪功能。但是,如果所述事件发生,客户应该能够检查。