Edit report at https://bugs.php.net/bug.php?id=62359&edit=1
ID: 62359
Comment by: mail+php at requinix dot net
Reported by: vogliadifarniente at gmail dot com
Summary: __call not called for private __construct when
creating a new instance
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Linux (Debian)
PHP Version: 5.4.4
Block user comment: N
Private report: N
New Comment:
__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.
Previous Comments:
------------------------------------------------------------------------
[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