Req #44604 [Opn]: newInstanceArgs (ReflectionClass) and visibility
| From: | requinix@php.net | Date: | Wed, 20 Nov 2013 11:02:56 +0000 |
| Subject: | Req #44604 [Opn]: newInstanceArgs (ReflectionClass) and visibility | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182853@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=44604&edit=1
ID: 44604
Updated by: requinix@php.net
Reported by: vituko at gmail dot com
Summary: newInstanceArgs (ReflectionClass) and visibility
Status: Open
Type: Feature/Change Request
Package: Reflection related
Operating System: Debian GNU/Linux
PHP Version: 5.3CVS-2008-04-02 (CVS)
Block user comment: N
Private report: N
New Comment:
As of PHP 5.4 you can use ReflectionClass::newInstanceWithoutConstructor() and manually invoke the
constructor.
$rc = new ReflectionClass("foo");
$obj = $rc->newInstanceWithoutConstructor();
$ctor = $rc->getConstructor();
if ($ctor) { // constructors are optional!
$ctor->setAccessible(true);
$ctor->invoke($obj);
}
A couple more function calls seems a small price to pay for the ability to do something the class
itself specifically tried to prohibit you from doing.
Previous Comments:
------------------------------------------------------------------------
[2013-11-20 10:39:51] jerome at taotesting dot com
I would also argue that such a feature would be absolutely great for Unit Testing. If method calls
can be done on protected/private methods via ReflectionMethod::setAccessible(), why not allow that
with private constructors?
All the best,
Jérôme
------------------------------------------------------------------------
[2008-04-04 18:00:44] jani@php.net
Reclassified. NOTE: Discussion about this should be done on the internals mailing list..
------------------------------------------------------------------------
[2008-04-04 14:06:57] vituko at gmail dot com
I didn't say it was a bug.
I only said that it is a limitation... maybe easy to change.
ReflectionClass -> newInstanceArgs is very nice but only works with public constructors. If a
class constructor is private, for example, you cannot instantiate it with this method, even from
this class scope.
The same example with call_user_func_array, you change the constructor with another method and it
works... But this function doesn't allow to instantiate an object (you can only call a parent
constructor on an already instantiated object).
So, in this context (a class with a private/protected constructor), the only way of instantiating it
with an arbitrary number of arguments is to build a string and use eval... not very nice nor
performant.
Thanks
------------------------------------------------------------------------
[2008-04-03 11:23:41] jani@php.net
And the bug here is...?
------------------------------------------------------------------------
[2008-04-02 08:14:56] vituko at gmail dot com
Description:
------------
1 - call_user_func_array ($ref_f, $taboa) ;
Here, the visibility is computed from the context where this
function is called.
2 - $obx -> newInstanceArgs ($taboa) ;
ReflectionException : Access to non-public constructor of class ...
Constructor must be public anyway.
So ... eval is not so bad.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=44604&edit=1