Given the class Foo with an old-style constructor
class Foo
{
public function Foo()
{
//does constructing stuff
}
}
Is there any functional difference between calling the parent constructor with a new style constructor or the old style constructor?
class Bar extends Foo
{
public function Bar()
{
//does it matter?
//parent::__construct();
//parent::Foo();
}
}
Put another way, is there anything special about the static call
parent::__construct()
when it’s made from a constructor, or is it just a standard static call?
Before the Best Practices Flying Monkeys descend, I’m dealing with some legacy code and trying to understand the consequences of everything that’s going on.
I would say both syntax do exactly the same thing…
Edit : after writting the rest of the answer, actually, this is not entirely true ^^ It depends on what you declare ; see the two examples :
If you define
Fooas constructor, and call it with__construct, it seems it’s working ; the following code :Outputs
So, all OK for now 😉
On the other way, if you define __construct, and call Foo, like this :
It’ll get you a Fatal Error :
So, if your class is declared with old-syntax, you can call it both ways ; and if it’s defined with new (PHP5) syntax, you must use that new syntax — which makes sense, afterall 🙂
BTW, if you want some kind of “real proof”, you can try using the Vulcan Logic Disassembler, that will give you the opcodes corresponding to a PHP script.
EDIT after the comment
I’ve uploaded the outputs of using VLD with both syntaxes :
– vld-construct-new.txt : when declaring __construct, and calling __construct.
– vld-construct-old.txt : when declaring Foo, and calling __construct.
Doing a diff between the two files, this is what I get :
(Unified diff is much longer, so I’ll stick to using the default format of “diff” here)
So, the only differences in the disassembled opcodes are the names of the functions ; both in the
Fooclass and in theBarclass (that inherits the__construct/Foomethod of classFoo).What I would really say is :
As the sidenote, the documentation says (quoting) :
So, I really think there is not that much of a difference 🙂
Did you encounter some kind of strange problem, that you think is caused by something like a difference between the two syntaxes ?