#49376 [Bgs]: ReflectionClass::newInstanceArgs cannot instantiate a constructorless class
| From: | sebcsaba at freemail dot hu | Date: | Mon, 14 Sep 2009 18:22:27 +0000 |
| Subject: | #49376 [Bgs]: ReflectionClass::newInstanceArgs cannot instantiate a constructorless class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-142356@lists.php.net to get a copy of this message | ||
ID: 49376
User updated by: sebcsaba at freemail dot hu
Reported By: sebcsaba at freemail dot hu
Status: Bogus
Bug Type: Reflection related
Operating System: WinXP
PHP Version: 5.3.0
New Comment:
Thank you for taking the time to write to me, but if this is not a bug
then this is a bad design of the method.
I've double-checked the documentation available at
http://php.net/manual/en/reflectionclass.newinstanceargs.php
which said
'This function is currently not documented; only its argument list is
available.' and said nothing about the numbers of the element in the
given array.
If you want to make a factory function with vararg, then you don't want
to case about the number of the parameters.
I think this should work:
function factory($className /* and other params here... */ ) {
$rc = new ReflectionClass($className);
$params = func_get_args();
array_shift($params);
return $rc->newInstanceArgs($param);
}
The new $className(...) construction doesn't work here.
What is the difference between
class A {}
and
class A { function __construct(){} }
from the view of the construction? Both can be constructed by a
zero-arg constructor call, new A(). If I've an array containing zero
elements, why should not the $rc->newInstanceArgs($array) works in the
first case, even it does in the second?
Previous Comments:
------------------------------------------------------------------------
[2009-08-28 23:14:03] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Do not pass any param to method. Just use $rc->newInstanceArgs();
------------------------------------------------------------------------
[2009-08-26 16:12:00] sebcsaba at freemail dot hu
Description:
------------
I've got a class without a constructor, and I want to instantiate it by
ReflectionClass::newInstanceArgs with an empty array as arguments.
This causes:
ReflectionException: 'Class A does not have a constructor, so you
cannot pass any constructor arguments'.
If I define an empty default constructor, this works well.
Reproduce code:
---------------
class A {}
$rc = new ReflectionClass('A');
$a = $rc->newInstanceArgs(array());
Expected result:
----------------
Create a new instance of class A, using default constructor behaviour.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49376&edit=1