我正在尝试执行一些函数,它将查找传递的变量并从给定的类中查找给定方法的必需参数,并尝试匹配它们,这样您就不必按特定顺序传递变量。当我通过所有变量时,该功能运行良好,但是当我错过了一些var to pas并且期望方法给出默认值时我会发现错误
致命错误:/share/HDA_DATA/Web/framework/framework.php:45中未捕获的异常'ReflectionException',消息'参数不是可选的'堆栈跟踪:#0 / share / HDA_DATA / Web / framework / framework。 php(45):ReflectionParameter-> getDefaultValue()#1 /share/HDA_DATA/Web/framework/framework.php(59):在/ share / HDA_DATA / Web / framework / framework中抛出passValues()#2 {main}第45行的.php
这里我有我的代码,我称之为
www.domainname.com/index.php?class=person&method=identification&surname=Somesurname&somestrangevar=justtest
class foo
{
public function bar($one, $two, $stupid, $somethingelse)
{
echo $one.$two.$stupid.$somethingelse;
}
public function zar($one,$somethingelse)
{
echo $one.$somethingelse;
}
}
class person
{
public function identification($name = 'Smith', $surname)
{
echo "Name: ".$name."<br/>";
echo "Surname: ".$surname."<br/>";
}
}
$method = new ReflectionMethod($_GET['class'], $_GET['method']);
$num = $method->getParameters();
function passValues()
{
global $method;
$arguments = array();
foreach($method->getParameters() as $value)
{
if(isset($_GET[$value->name]) && $_GET[$value->name] !="")
{
$arguments[$value->name] = $_GET[$value->name];
}
else
{
if($value->isDefaultValueAvailable())
{
$arguments[$value->name] = $value->getDefaultValue();
}
else
{
echo "Please give value to parameter '".$value->name."'.";
return 0;
}
}
}
$myFoo = new $_GET['class'];
call_user_func_array(array($myFoo, $_GET['method']) , $arguments);
}
passValues();
使用PHP版本5.3.9