ReflectionClass::newInstance

(PHP 5)

ReflectionClass::newInstanceCreates a new class instance from given arguments.

Description

public object ReflectionClass::newInstance ( mixed $args [, mixed $... ] )

Creates a new instance of the class. The given arguments are passed to the class constructor.

Parameters

args

Accepts a variable number of arguments which are passed to the class constructor, much like call_user_func().

Return Values

Errors/Exceptions

A ReflectionException if the class constructor is not public.

A ReflectionException if the class does not have a constructor and the args parameter contains one or more parameters.

See Also

Коментарии

looks like reflection class newInstance creates in memory representation of code where values are used, so using reference as constructor signature, you can not use this method.

as  the same input if called via new, or new $class works, but not via reflection:

 class a {
     public function __construct(&$a, $c) {
     }
 }

// this works
 $A = new stdClass();
 $a = new a($A, 11);

 // also this works
 $classname = "a";
 $a = new $classname($A, 10);

 // but this fails:
 $r = new ReflectionClass("a");
 $r->newInstance($A, 10);

 PHP Warning:  Parameter 1 to a::__construct() expected to be a reference, value given in reflection.php on line 15

 PHP Warning:  ReflectionClass::newInstance(): Invocation of a's constructor failed in reflection.php on line 15
2015-10-07 14:19:31
http://php5.kiev.ua/manual/ru/reflectionclass.newinstance.html

    Поддержать сайт на родительском проекте КГБ