http://php.net/manual/en/reflectionmethod.invokeargs.php
<?php
class HelloWorld {
public function sayHelloTo($name,$test) {
return 'Hello ' . $name." test :".$test;
}
/**
* 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);
}
}
$obj=new HelloWorld();
echo $obj->__named('sayHelloTo', array('test'=>'test','name'=>'Mike'));
// $reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
// echo $reflectionMethod->invokeArgs(new HelloWorld(), array('test1'=>'test','name'=>'Mike'));
?>
PHP 根据参数名称调用php方法
最新推荐文章于 2023-03-02 10:17:30 发布