我想检查一个方法是否存在,并且是否有参数。
以下是一些代码片段。
// this only checks if the function exist or not
if(method_exists($controller, 'function_name'))
{
//do some stuff
}
但是我想要做的是什么?
if(method_exists($controller, 'function_name(with_args)'))
{
}
我想检查一个方法是否存在,并且是否有参数。
以下是一些代码片段。
// this only checks if the function exist or not
if(method_exists($controller, 'function_name'))
{
//do some stuff
}
if(method_exists($controller, 'function_name(with_args)'))
{
}
ReflectionMethod
的getParameters
方法获取一个方法的参数列表。您可以检查该列表的长度或对参数执行任何其他操作。<?php
class Foo {
function bar($a, $b, $c) {
return $a + $b + $c;
}
}
$method = new ReflectionMethod('Foo', 'bar');
var_dump($method->getParameters());
?>
我不知道你会用这个做什么,但我建议不要随意使用反射。你可能需要重新思考你的做法。
函数重载是一种功能,允许创建几个具有相同名称但在输入和输出类型上不同的方法。不幸的是,PHP中没有实现这个功能,您不能使用方法重载来拥有两个具有相同名称的函数。
为什么要使用function_exists来使用函数重载呢?