Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a doubt in Php class initialization.. Like in Java, an instantiated class calls constructor of parent classes to initialize the parent classes to be able to use their properties, PHP need not require us to call Parent Class constructor in order to be able to use its Properties and method.. Like in the code :
class Mammal{
    protected $name = "Hey";
    function __construct(){
        echo "Class of Mammals";
     }
}
class Dog extends Mammal{
    public $ecolor = "Black";
    public $fcolor = "White";
    public $nose = "Sharp";
    function __construct(){
        echo "Lovely Dog <br>";
        echo $this->name . "<br>"; // can call name without parent::__construct()
}
    function showAll(){
        echo "Eye Color : " . $this->ecolor . " fcolor : " . $this->fcolor . " nose : " . $this->nose;
    }
}
How are we able to call $name variable of Mammal without initializing it using constructor?

What I have tried:

PHP parent constructor initialization
Posted
Updated 2-Jul-18 5:50am

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.
 
Share this answer
 
<?php
class BaseClass {
    function __construct() {
        print "In BaseClass constructor\n";
    }
}

class SubClass extends BaseClass {
    function __construct() {
        parent::__construct();
        print "In SubClass constructor\n";
    }
}

class OtherSubClass extends BaseClass {
    // inherits BaseClass's constructor
}

// In BaseClass constructor
$obj = new BaseClass();

// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();

// In BaseClass constructor
$obj = new OtherSubClass();
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900