Edit report at https://bugs.php.net/bug.php?id=64454&edit=1
ID: 64454 User updated by: wolters dot fl at gmail dot com Reported by: wolters dot fl at gmail dot com Summary: It is possible to directly call the magic method "__construct". Status: Wont fix Type: Feature/Change Request Package: Class/Object related Operating System: Microsoft Windows Version 6.2.9 PHP Version: 5.4.13 Block user comment: N Private report: N New Comment: Yes, I do understand that "__construct" is an interceptor method and can be called from a subclass directly via "parent::__construct", but: 1. "parent::__construct" is a "static" method call which can occur inside of a class only. 2. The problem is, that it is possible to call "$obj->__construct" from outside a class. I could understand the behaviour if the call would be allowed from the inside of a class only. Imho, calling "__construct" directly should ONLY be possible from the INSIDE of a class. Plus, such a call MUST be STATIC (otherwise a PHP error is raised). >From a clients PoV, __construct is a member method (similar to __clone): * __clone is invoked via the "clone" operator. * __construct is invoked via the "new" operator. I don't see the reason, why there is a difference in that behaviour. I do not know much about the internals (C code) of the interceptor methods, but from the clients PoV these distinctions in behaviour are confusing. PS: I also updated my example at https://gist.github.com/FlorianWolters/5195734 to emphasize this. Previous Comments: ------------------------------------------------------------------------ [2013-03-22 07:03:54] ni...@php.net Additionally to what laruence said, one should also point out that __construct is quite commonly called directly in the form of parent::__construct(). ------------------------------------------------------------------------ [2013-03-22 04:18:32] larue...@php.net PHP's __construct, actually, is not creating object, it decorate the object after it was created. so, even you directly call to $obj->__construct, then no new instance was create. so a workaround is you can exam some property you set to see whether that is a double constructing or not. thanks ------------------------------------------------------------------------ [2013-03-19 12:45:54] wolters dot fl at gmail dot com Description: ------------ It is possible to directly call the magic method "__construct". This should NOT be possible, since one cannot create immutable objects with the current behaviour. In addition, a direct call to the magic method "__clone" is not possible and raises an "\E_FATAL". Both magic methods deal with object creation, so the behaviour is inconsistent between them. I am using "php-5.4.13-nts-Win32-VC9-x86" on Microsoft Windows Version 6.2.9 x64(Windows 8 64 bit). Test script: --------------- https://gist.github.com/FlorianWolters/5195734 Expected result: ---------------- The method call: $example->__construct(); should raise an "\E_FATAL" with the following message: "Cannot call __construct() method on objects - use 'new <Class>' instead in [...] on line 21" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=64454&edit=1