php - 如何获取运行时确定的可调用参数的数量?

标签 php reflection callback

注意:通过写这个问题,我已经发现我对使用新的语言功能过于热心了。更简洁的解决方案是改用策略模式……不过,我还是很好奇是否有解决此问题的正确方法。

TL;DR:您能否反射(reflection) PHP 中的泛型 Callable,而无需手动对所有类型的 Callable 进行类型检查?

在 PHP 5.4 中,我们有一个新的类型提示:可调用。这看起来很有趣。我想我会通过以下方式利用它:

<?php
    public function setCredentialTreatment(callable $credentialTreatment) {
       // Verify $credentialTreatment can be used (ie: accepts 2 params)
       ... magic here ...
    }
?>

到目前为止,我的想法是对可调用对象进行一系列类型检查,并从中推断出要使用哪个 Reflection* 类:

<?php
if(is_array($callable)) {
    $reflector = new ReflectionMethod($callable[0], $callable[1]);
} elseif(is_string($callable)) {
    $reflector = new ReflectionFunction($callable);
} elseif(is_a($callable, 'Closure') || is_callable($callable, '__invoke')) {
    $objReflector = new ReflectionObject($callable);
    $reflector    = $objReflector->getMethod('__invoke');
}

// Array of ReflectionParameters. Yay!
$parameters = $reflector->getParameters();
// Inspect parameters. Throw invalidArgumentException if not valid.
?>

现在,对我来说,这感觉太复杂了。我是否缺少某种捷径来实现我在这里想要做的事情?欢迎任何见解:)

最佳答案

我创建了一个更短的版本,它的工作方式与 call_user_func() 非常相似,并在 PHP 手册中针对回调/可调用对象的所有不同类型对其进行了测试。这样您几乎可以在任何地方使用它。 call_user_func() 不只接受对象,而且我认为这个函数也没有意义,因为它只处理回调。

下面包含有关如何使用它的测试和建议,希望对您有所帮助:

function getNrOfParams($callable)
{
    $CReflection = is_array($callable) ? 
    new ReflectionMethod($callable[0], $callable[1]) : 
    new ReflectionFunction($callable);
    return $CReflection->getNumberOfParameters();
}

整个测试及其结果:

<?php   
class Smart
{
    public function __invoke($name)
    {

    }

    public function my_callable($one, $two, $three)
    {

    }

    public static function myCallableMethod($one, $two) 
    {

    }

    public static function who()
    {
            echo "smart the parent class";
    }
}

class Smarter extends Smart
{
    public static function who()
    {
        echo "smarter";
    }
}

function my_ca($one)
{

}

function getNrOfParams($callable)
{
    $CReflection = is_array($callable) ? new ReflectionMethod($callable[0], $callable[1]) : new ReflectionFunction($callable);
    return $CReflection->getNumberOfParameters();
}
// Test 1 Callable Function
echo "Test 1 - Callable function:" . getNrOfParams('my_ca');

// Test 2 Static method
echo " Test 2 - Static class method:" . getNrOfParams(array('Smart', 'myCallableMethod'));

// Test 3 Object method
$smart = new Smart();
echo " Test 3 - Object method:" . getNrOfParams(array($smart, 'my_callable'));

// Test 4 Static method call (As of PHP 5.2.3)
//echo " Test 4 - Static class method call:" . getNrOfParams('Smart::myCallableMethod');
// Calling a static method this way does not work in ReflectionFunction.
// However, Test 2 provides a solution.

// Test 5 Relative static method (As of PHP 5.3.0)
//echo " Test 5 - Relative static class method:" . getNrOfParams(array('Smarter', 'parent::who'));
// Calling a relative static method doesn't work either. ReflectionMethod lacks support.
// All other tests work.

// Tesy 6 __invoke
echo " Test 6 - __invoke:" . getNrOfParams(array($smart, '__invoke'));

// Test 7 Closure
$closure = function($one, $two, $three)
{
    // Magic
};
echo " Test 7 - Closure:" . getNrOfParams($closure);

关于php - 如何获取运行时确定的可调用参数的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071186/

相关文章:

php - 加载 Magento 页面时出现奇怪的错误

php - 在 Qt C++ 中使用 openssl 解码和编码文本

PHP mkdir 权限

c# - obj.GetType().IsInterface 可以是真的吗?

c# - Activator.CreateInstance MissingMethodException 异常

Javascript - 在此示例中,异步并行中的回调发生了什么?

php - 如何将用户输入与自定义 WordPress 数据库表进行比较

java - 使用java反射获取对象的特定属性

android - 只有在另一个类中完成使用 Parse.com 的异步数据导入后,才在 Android 中填充 ListView

user-interface - 在 MATLAB 中,如何在拖动 slider 时执行回调?