Monday, 3 April 2017

Constructor

Constructor
void __construct ([ mixed $args = "" [, $... ]] )
The constructor is the most useful of the two, especially because it allows you to send parameters along when creating a new object, which can then be used to initialize variables on the object
PHP  allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
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).

class Animal
{
    public $name = "No-name animal";
   
    public function __construct()
    {
        echo "I'm alive!";       
    }
}

Output


$animal = new Animal(); 

No comments:

Post a Comment