class A {
private $aa;
protected $bb = 'parent bb';
function __construct($arg) {
//do something..
}
private function parentmethod($arg2) {
//do something..
}
}
class B extends A {
function __construct($arg) {
parent::__construct($arg);
}
function childfunction() {
echo parent::$bb; //Fatal error: Undefined class constant 'bb'
}
}
$test = new B($some);
$test->childfunction();
Question:
How do I display the parent variable in the child?
the expected result will echo ‘parent bb’
The variable is inherited and is not private, so it is a part of the current object.
Here is additional information in response to your request for more information about using
parent:::Use
parent::when you want add extra functionality to a method from the parent class. For example, imagine anAirplaneclass:Now suppose we want to create a new type of Airplane that also has a navigator. You can extend the __construct() method to add the new functionality, but still make use of the functionality offered by the parent:
In this way, you can follow the DRY principle of development but still provide all of the functionality you desire.