访问已被父 class 方法修改的子 class 中的 属性
Accessing a property in a child class which has been modified by a parent class method
我正在处理一个应用程序,我 运行 陷入了这个困境。下面是一个非常简化的版本,但我想知道它是否可行
示例:
class A{
public $test = null;
public function method1(){
$this->test = 'finished';
}
}
class B extends A{
public function getMethod1Test(){
return $this->test;
}
}
$a = new A();
$a->method1();
$b = new B();
$b->getMethod1Test(); //this currently returns NULL
如何修改它,使其 returns 值 'finished' 而无需将修改后的值重新插入 class B?
谢谢
如果你创建 class 这样的 A..
class A{
public $test = null;
public function __construct(){ // constructor
$this->test = 'finished';
}
}
虽然我还没有测试过,但应该可以。
我正在处理一个应用程序,我 运行 陷入了这个困境。下面是一个非常简化的版本,但我想知道它是否可行
示例:
class A{
public $test = null;
public function method1(){
$this->test = 'finished';
}
}
class B extends A{
public function getMethod1Test(){
return $this->test;
}
}
$a = new A();
$a->method1();
$b = new B();
$b->getMethod1Test(); //this currently returns NULL
如何修改它,使其 returns 值 'finished' 而无需将修改后的值重新插入 class B?
谢谢
如果你创建 class 这样的 A..
class A{
public $test = null;
public function __construct(){ // constructor
$this->test = 'finished';
}
}
虽然我还没有测试过,但应该可以。