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:
Thanks a lot for this reply !
Even if from a pure OOP perspective, it looks strange to me... If it works, I'm can go with it
!
To be a little bit more precise about why such a feature is important in a unit testing context:
When implementing pure Composition (strong aggregation), it is forbidden to instantiate a composite
class if container is not instantiated. However, if you work in a bottom to top approach,
you'll have to be able to instantiate this composite class for testing, before to program and
test the container class.
Bests
Previous Comments:
------------------------------------------------------------------------
[2013-11-20 11:02:56] requinix@php.net
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.
------------------------------------------------------------------------
[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...?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=44604
--
Edit this bug report at https://bugs.php.net/bug.php?id=44604&edit=1
Thread (8 messages)