I have a parent class and a subclass, the parent class has a constructer that sets a var
and I would like to use that var
in the subclass, I have it working but am getting confused by the keyword parent
?
Example
class Sub extends Parent {
public function foo() {
echo $this -> myVar;
}
}
class Parent {
var $myVar;
public function __construct() {
$this -> myVar = 'a';
}
}
This worked and I get the value of myVar
, but am I supposed to be using the keyword parent
and when I do I get an error, example,
class Sub extends Parent {
public function foo() {
echo parent -> myVar;
}
}
class Parent {
var $myVar;
public function __construct() {
$this -> myVar = 'a';
}
}
See Question&Answers more detail:os