isholao/callableresolver

PHP可调用解析器

1.0 2017-11-21 05:05 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:44:38 UTC


README

Build Status

安装

使用composer安装

composer require isholao/callableresolver

需要PHP 7.1或更高版本。

使用方法

以下是一个基本的使用示例

<?php

require '/path/to/vendor/autoload.php';

class Dummy {
    function methodToCall(){
        return 'methodToCall';
    }
}

$resolver = new \Isholao\CallableResolver\Resolver();
$resolved = $resolver->resolve(Dummy::class.'->methodToCall'); 

\\or

$resolved = $resolver->resolve(function(){
    return 'methodToCall';
});

$resolved(); // 'methodToCall'

该库提供了一个有用的辅助工具,用于使用有效的PHP字符串调用类,例如 'Class->method'

<?php

require '/path/to/vendor/autoload.php';

class Dummy {
    function methodToCall($name){
        return $name;
    }
}

$dc = new \Isholao\CallableResolver\DeferredCallable(Dummy::class.'->methodToCall');
$dc('methodToCall'); // 'methodToCall'

//or

$dc = new \Isholao\CallableResolver\DeferredCallable(Dummy::class.'->methodToCall', new \CallableResolver\Resolver());
$dc('methodToCall'); // 'methodToCall'