Bug #62359 [Opn->Nab]: __call not called for private __construct when creating a new instance
| From: | cataphract@php.net | Date: | Tue, 19 Jun 2012 08:36:22 +0000 |
| Subject: | Bug #62359 [Opn->Nab]: __call not called for private __construct when creating a new instance | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-171364@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62359&edit=1
ID: 62359
Updated by: cataphract@php.net
Reported by: vogliadifarniente at gmail dot com
Summary: __call not called for private __construct when
creating a new instance
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Linux (Debian)
PHP Version: 5.4.4
Block user comment: N
Private report: N
New Comment:
You can't override the creation of an object in that sense. No such feature exists. Hence, not
a bug.
Previous Comments:
------------------------------------------------------------------------
[2012-06-18 21:18:36] mail+php at requinix dot net
__construct isn't just some special method: it's the constructor. By making it
private you are disabling construction outside the class - __call can't be
executed because the object can't even be created in the first place.
So now you think "Okay, well allow construction if there's a __call". But that's
not an option because then any class that has a __call can be constructed
regardless of whether the constructor is private. "Okay, then only allow it if
__call handles the '__construct' method". But how is PHP supposed to know that?
It can't. Plus, now __call doesn't know whether the object has been fully
instantiated yet or whether it's executing before then, and that's a headache
all by itself.
Next idea is making __call execute both statically and as an instance method,
but that's not a good idea either: how is __call supposed to know when it's
static or not? Don't forget there's __callStatic too.
And now we arrive at the idea of making a static method that acts as a special
kind of constructor. Which is what getSingleton() does.
------------------------------------------------------------------------
[2012-06-18 21:00:02] vogliadifarniente at gmail dot com
Description:
------------
I'm trying to override the creation of a class instance making __construct private and using
the __call method (like the operatornew method of C++).
Test script:
---------------
http://pastebin.com/D0k20NiV
Expected result:
----------------
No error and like other example print 32 then 45 then 32.
Actual result:
--------------
Fatal error: Call to private MyClass::__construct() from invalid context
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62359&edit=1