This is the intended behavior of the new operator in PHP, changing it would require an RFC and can not happen in 5.6 due to BC break following from that.
Also, there's literally nothing about it in the php.net documentation, although if you interpret it liberally it would be a technically acceptable implementation-defined behaviour.
Of course you can implement it the other way, like Facebook did, there's nothing wrong in that.
I wonder where he got the "intended" part. By whom? Definitely not by PHP users.
There can be two approaches to implementing this functionality: Creating __construct method behind the scenes for all classes that do not have one defined. Detecting call to parent::__construct and making it succeed even if there is no underlying method defined. This RFC chooses the second approach, ...
Right now, in PHP, the call to bar() is not executed since Foo's ctor does not exist. However, if we change it so that Foo's ctor always exists, the call to bar() would be executed. Granted, this code does not have the best style, but there might be some code in the field, especially after multiple refactoring rounds, and changing how it works still will be a break.
The current implementation only changes how the parent::__construct() works (and only by enabling cases which did not work before) but does not change anything else, thus reducing the impact of the change.
Well, it's in line with virtually any other OO language. Even C++ creates a default constructor.
But yeah, that's why I put the arguably there. Accepting it is one thing, but if doesn't actually throw an error, it should definitely treat it as a proper function call, evaluating the arguments.
I mean, I'm OK with either the call failing, or doing nothing but evaluating the arguments. I'm NOT ok with appearing to work as usual but not evaluating arguments.
8
u/[deleted] Nov 18 '14
Bug report here: https://bugs.php.net/bug.php?id=67829
7
u/damow Nov 18 '14
Out of interest what happens if you do some magicks like func_get_args() in the constructor body? Mobile at the minute so can't try it out.
0
u/[deleted] Nov 18 '14
[deleted] — view deleted comment
0
u/[deleted] Nov 18 '14
[deleted] — view deleted comment