One thing I figured out after a long time about extending a parent class that, if the child class does not have any construct function, it will use its parent's construct.
for example:
<?php
class Main
{
public $a;
public function __construct()
{
echo '::Parent Class initiated::';
$this -> a = 'we are in the parent class';
}
}
class Child extends Main
{
public function getA()
{
return $this -> a;
}
}
$main = new Main();
$main -> child = new Child;
echo $main -> child -> getA();
//Output - ::Parent Class initiated::::Parent Class initiated::we are in the parent class
?>
However, If we have a constructor in the child class as well:
<?php
class Child extends Main
{
public function __construct()
{
}
public function getA()
{
return $this -> a;
}
}
?>
Then :
<?php
$main = new Main();
$main -> child = new Child;
echo $main -> child -> getA();
// Output - ::Parent Class initiated::
?>
Note that the parent variable 'a' is not inherited by the child class if the constructor from the parent class isnt called.
This behaviour of extension made me waste a lot of my precious time, as I could not understand why some of my child classes were inheriting parent variables and some were not.
Hope this helps someone..