Req #44604 [Com]: newInstanceArgs (ReflectionClass) and visibility
Edit report at https://bugs.php.net/bug.php?id=44604&edit=1
ID: 44604
Comment by: jerome at taotesting dot com
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:
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
Previous Comments:
------------------------------------------------------------------------
[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
Thread (8 messages)