downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ReflectionClass::setStaticPropertyValue> <ReflectionClass::newInstance
Last updated: Fri, 30 Oct 2009

view this page in

ReflectionClass::newInstanceArgs

(PHP 5 >= 5.1.3)

ReflectionClass::newInstanceArgsNew instance args

Beschreibung

public object ReflectionClass::newInstanceArgs ([ array $args ] )

New instance args.

Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.

Parameter-Liste

args

Rückgabewerte

Siehe auch



add a note add a note User Contributed Notes
ReflectionClass::newInstanceArgs
kevinpeno at gmail dot com
16-Sep-2009 03:11
I misunderstood this function to be a sort of setter of Reflection::newInstance() arguments in an array form rather than a creator of new instances itself.

This function is equivilant to call_user_func_array() while Reflection::newInstance() is equivilant to call_user_func()
richardcook at gmail dot com
12-Apr-2009 09:39
the newInstanceArgs function cannot call a class' constructor if it has references in its arguments, so be careful what you pass into it:

<?php
class Foo {
    function
__construct (&$arr) {
       
$this->arr = &$arr;
    }
    function
createInstance () {
       
$reflectionClass = new ReflectionClass("Bar");
       
        return
$reflectionClass->newInstanceArgs(array($this, $this->arr));
    }
    function
mod($key, $val) {
       
$this->arr[$key] = $val;
    }
}

class
Bar {
    function
__construct (&$foo, &$arr) {
       
$this->foo = &$foo;
       
$this->arr = &$arr;
    }
    function
mod($key, $val) {
       
$this->arr[$key] = $val;
    }
}

$arr = array();

$foo = new Foo($arr);

$arr["x"] = 1;

$foo->mod("y", 2);

$bar = $foo->createInstance();

$bar->mod("z", 3);

echo
"<pre>";
print_r($arr);
print_r($foo);
print_r($bar);
echo
"</pre>";

/*
Output:
Warning: Invocation of Bar's constructor failed in [code path] on line 31

Fatal error: Call to a member function mod() on a non-object in [code path] on line 58
*/
?>
gromit at mailinator dot com
29-Jul-2008 08:37
Be aware that calling the method newInstanceArgs with an empty array will still call the constructor with no arguments. If the class has no constructor then it will generate an exception.

You need to check if a constructor exists before calling this method or use try and catch to act on the exception.

 
show source | credits | sitemap | contact | advertising | mirror sites