Hi stackOverflow Family :),
I have a question, and I didt find the answer elsewhere. I try to explain my problem:
I have a class, and if I create an other class from it, from that child class I couldnt access the parent’s properties.
I did something wrong?
I tried to copy my class variable to a local and try to give back that local one, but neither works of the following 3 way.
Here is my examples.
At first I simple create an object:
$test = new test();
And my two class is the following:
class test {
public $testvar;
public function __construct() {
$this->testvar = 1234568;
echo ":) ".$this->testvar();
$test2 = new test2();
}
public function testvar() {
echo "testvar() called > ";
return $this->testvar;
}
}
And test2:
class test2 extends test {
public function __construct() {
echo "<br>:| this-> ".$this->testvar;
echo "<br>:| parent:: ". parent::testvar();
echo "<br>:| "; $this->testvar();
}
}
May somebody have an idea?
Thx
You’ve misunderstood the inheritance concept. Instantiating
test2in the constructor function oftestis not inheritance.The constructor of
testwas not called, thereforetestvarwas not set. Remove$test2 = new test2();from the constructor oftest. Try:See also the PHP manual on constructors (and classes too).