PHP Doku:: Invoke args - reflectionmethod.invokeargs.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenReflectionThe ReflectionMethod classReflectionMethod::invokeArgs

Ein Service von Reinhard Neidl - Webprogrammierung.

The ReflectionMethod class

<<ReflectionMethod::invoke

ReflectionMethod::isAbstract>>

ReflectionMethod::invokeArgs

(PHP 5 >= 5.1.0)

ReflectionMethod::invokeArgsInvoke args

Beschreibung

public mixed ReflectionMethod::invokeArgs ( object $object , array $args )

Invoke arguments.

Parameter-Liste

object

The object to invoke the method on. In case of static methods, you can pass null to this parameter.

args

The parameters to be passed to the function, as an array.

Rückgabewerte

Returns the method result.

Fehler/Exceptions

A ReflectionException if the object parameter does not contain an instance of the class that this method was declared in.

A ReflectionException if the method invocation failed.

Beispiele

Beispiel #1 ReflectionMethod::invokeArgs() example

<?php
class HelloWorld {

    public function 
sayHelloTo($name) {
        return 
'Hello ' $name;
    }

}

$reflectionMethod = new ReflectionMethod('HelloWorld''sayHelloTo');
echo 
$reflectionMethod->invokeArgs(new HelloWorld(), array('Mike'));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Hello Mike

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
serg dot smertin at gmail dot com
21.09.2010 15:51
We can do black magic, which is useful in templating block calls:

<?php
     $object
->__named('methodNameHere', array('arg3' => 'three', 'arg1' => 'one'));

     ...

     
/**
       * Pass method arguments by name
       *
       * @param string $method
       * @param array $args
       * @return mixed
       */
     
public function __named($method, array $args = array())
      {
       
$reflection = new ReflectionMethod($this, $method);

       
$pass = array();
        foreach(
$reflection->getParameters() as $param)
        {
         
/* @var $param ReflectionParameter */
         
if(isset($args[$param->getName()]))
          {
           
$pass[] = $args[$param->getName()];
          }
          else
          {
           
$pass[] = $param->getDefaultValue();
          }
        }

        return
$reflection->invokeArgs($this, $pass);
      }
?>
addiks at gmx dot de
20.05.2010 17:14
it seems that ReflectionMethod::invodeArgs() dont like reference-arguments in the method to call:

<?php

class Test{
  function
foo(&$arg){
   
$arg++;
  }
}

$test = new Test();

$class = new ReflectionClass("Test");
$method = $class->getMethod("foo");

$bar = 9;

$method->invokeArgs($test, array($bar));
?>

RESULT: "Invocation of method Test::foo() failed"



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)