Reputation: 39
I was playing around with a couple of classes to understand the relationship between parent and child. I setup the parent to have a constructor that calls an init method. Then when I add an init method to the child, it should override the parent init, shouldn't it? But what is happening is that both methods are being called.
To test this, I wrote a class named Model and a child called Instance. Here is the code:
$try = new Instance;
echo $try;
class Model{
public function __construct(){
$this->init();
}
public function init()
{
return $this->className();
}
public function __toString()
{
return $this->className();
}
public static function className()
{
return get_called_class();
}
}
class Instance extends Model
{
public function init()
{
echo "tada! ";
}
}
Gives the following output:
tada! Instance.
In class Model, I use the magic method __toString() to return the class name as a string. The constructor of the parent calls the parent init() method, which in this case echoes the class name.
My understanding is that if I write a child class, in this case the class is called Instance, with an init() method, it would overwrite the parent init() method, but that is not what is happening. In this case, it returns both init mehtods and I have no idea why. Can anyone explain this?
Upvotes: 3
Views: 1802
Reputation: 446
The fact is -
When you instantiate an object of Class "Instance" using "$try = new Instance;". it calls the constructor and since child class overrides "init" method, it prints "tada!".
On the other line you echo the object of Class "Instance" using "echo $try;" and a magic method __toString() implemented in parent class. So it print "Instance" as class name.
You can run only $try = new Instance; that will print only "tada!".
Upvotes: 3
Reputation: 18430
In this case, it returns both init mehtods
No it doesn't;
'tada!' is being echoed by Instance::init()
and 'Instance' is being echoed by Model::__toString()
.
Upvotes: 0
Reputation: 68556
Since you don't have any constructor defined for your child class, the parent class constructor will be fired and that is why you get that output.
When you do
$try = new Instance;
The parent class constructor gets fired and it will call the init()
which in turn calls the className()
that does the get_called_class()
, so eventually your init()
in the child class will be called and you get the output "tada!" first.
And when you do..
echo $try;
The __toString()
is called and returns you the class name Instance
So basically , If you had a constructor
on your child class , you will not be getting the output "tada!"
Upvotes: 0
Reputation: 35357
You are running two different commands. First you are instantiating the class with $try = new Instance;
which is calling __construct. The __construct() in the parent class is calling init() which is using the child init() and returning tada!
. Create a blank constructor in the child class to override the parent constructor.
The second command is when you echo the class: echo $try;
using __toString() which is echoing Instance
as defined in the parent class.
You can also add to a parent constructor by using:
public function __construct() {
...code...
parent::__construct();
...code...
}
Upvotes: 2