私はPHPでOOPを使用する方法を理解しようとしていますが、クラス継承に関するもののいくつかに少し困惑しています。
これらがどのように機能するかを学ぶために私が使用しているいくつかの簡単なクラスがあります。子クラスから呼び出し側(親)クラスに変数を設定したいだけです。私が読んだ例から、これは機能するはずですが、サブクラスは親変数を設定できません。
class test {
private $myvar;
private $temp;
function __construct(){
$this->myvar = 'yo!';
$temp = new test2();
$temp->tester();
echo $this->myvar;
}
public function setVar($in){
$this->myvar = $in;
}
}
class test2 extends test{
function __construct(){
}
public function tester(){
parent::setVar('yoyo!');
}
}
$inst = new test(); // expecting 'yoyo!' but returning 'yo!'
助けてくれてありがとう。