php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62359 __call not called for private __construct when creating a new instance
Submitted: 2012-06-18 21:00 UTC Modified: 2012-06-19 08:36 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: vogliadifarniente at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.4 OS: Linux (Debian)
Private report: No CVE-ID: None
 [2012-06-18 21:00 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-18 21:18 UTC] 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-19 08:36 UTC] cataphract@php.net
-Status: Open +Status: Not a bug
 [2012-06-19 08:36 UTC] cataphract@php.net
You can't override the creation of an object in that sense. No such feature exists. Hence, not a bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 02:02:52 2024 UTC