think\App::invokeMethod PHP Method

invokeMethod() public static method

调用反射执行类的方法 支持参数绑定
public static invokeMethod ( string | array $method, array $vars = [] ) : mixed
$method string | array 方法
$vars array 变量
return mixed
    public static function invokeMethod($method, $vars = [])
    {
        if (is_array($method)) {
            $class = is_object($method[0]) ? $method[0] : new $method[0](Request::instance());
            $reflect = new \ReflectionMethod($class, $method[1]);
        } else {
            // 静态方法
            $reflect = new \ReflectionMethod($method);
        }
        $args = self::bindParams($reflect, $vars);
        self::$debug && Log::record('[ RUN ] ' . $reflect->class . '->' . $reflect->name . '[ ' . $reflect->getFileName() . ' ]', 'info');
        return $reflect->invokeArgs(isset($class) ? $class : null, $args);
    }

Usage Example

Example #1
0
 public function testInvokeMethod()
 {
     $result = App::invokeMethod(['tests\\thinkphp\\library\\think\\AppInvokeMethodTestClass', 'run'], ['thinkphp']);
     $this->assertEquals('thinkphp', $result);
     $result = App::invokeMethod('tests\\thinkphp\\library\\think\\AppInvokeMethodTestClass::staticRun', ['thinkphp']);
     $this->assertEquals('thinkphp', $result);
 }
All Usage Examples Of think\App::invokeMethod