当更改Main类值时,id就像主类一样打印出更改后的值,而不是像单独的类一样,有没有办法通过使用接口或将一些控制器放在中间?
class Main {
private $test = 'init value';
public function set($str) {
$this->test = $str;
}
public function get() {
print($this->test);
}
}
class Sub extends Main{
public function setMain($str) {
$this->set($str); //set Main value
}
public function mainValue() {
print($this->get()); //get value from Main
}
}
$main = new Main();
$sub = new Sub();
$main->get(); //prints init value
$sub->setMain("end value");
$sub->mainValue(); //prints end value
$main->get(); //prints init value, but id like it to print out end value