Why the construct of the class Cars is being called as I have not created object for the class Cars?
<?php
class Cars{
function __construct(){
echo "i am cars";
}
}
class SmallCars extends Cars{
}
$car = new SmallCars();
?>
Why the construct of the class Cars is being called as I have not created object for the class Cars?
<?php
class Cars{
function __construct(){
echo "i am cars";
}
}
class SmallCars extends Cars{
}
$car = new SmallCars();
?>
When you extend a class, the child will inherit the parent's constructor unless you explicitly tells it not to
From the documentation:
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).
Also HReynaud has pointed this out in the comment section.
This question has been covered before in here
PHP __construct() documentation here