Quote:
Like in Java, an instantiated class calls constructor of parent classes to initialize the parent classes to be able to use their properties
This is not true for PHP when defining a constructor in a derived class:
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).
Your
$name
class member variable (called property with PHP) is not initialised by the constructor but upon object creation.
With PHP, each class property is initialised with a default value (usually
null
). The default value can be changed as done in your code by assigning a compile time constant value.
When a PHP object instance is created, the required memory is allocated first. Then all properties (including those of the parent classes) are initialised with their default values (which might be set by using initialisation with the declaration). Finally, the constructor is called if present.