__construct
The constructor is a magic method, which is immidately initated after declare the object of class and you can write the __construct method at any place in the class.
Right here all of us have a case in point during which, develop a school Any along with a constructor function within this particular school:
Here we take a example in which, create a class A and a constructor function inside this class:
class A{
public function __construct() {
$this->created = time();
}
}
Now we can create a class which inherits from the A class - a B! Without adding anything into the B class, we can declare it and have it inherit from A, like this:
class B extends A {
}
$b = new B;
echo $b->created;
If we define a __construct method in the B class, then B objects will run that instead when they are instantiated. Since there isn't one, PHP looks to the parent class definition for information and uses that. So we can override, or not, in our new class - very handy.
0 Comment(s)