有没有办法调用下面的方法,但只在末尾指定参数,并使用其他参数的默认值?
$published = true;
$this->display($published);
public function display($name = 'John', $date = 'December', $published = false){
//$name = 'John'
//$date = 'December'
//$published = true
}
答案 0 :(得分:0)
您可以使用反射,例如:
<?php
class A
{
public function display($name = 'John', $date = 'December', $published = false)
{
var_dump($name, $date, $published);
}
public function test()
{
$args = [];
$reflectionMethod = new ReflectionMethod($this, 'display');
foreach($reflectionMethod->getParameters() as $parameter) {
$args[$parameter->getName()] = $parameter->getDefaultValue();
}
$args['published'] = true;
$reflectionMethod->invokeArgs($this, $args);
}
}
(new A)->test();