invokeMethod() public static method

调用反射执行类的方法 支持参数绑定
public static invokeMethod ( string | array $method, array $vars = [] ) : mixed
$method string | array 方法
$vars array 变量
return mixed
Ejemplo n.º 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);
 }
Ejemplo n.º 2
0
 /**
  * REST 调用
  * @access public
  * @param string $method 方法名
  * @return mixed
  * @throws \Exception
  */
 public function _empty($method)
 {
     if (method_exists($this, $method . '_' . $this->method . '_' . $this->type)) {
         // RESTFul方法支持
         $fun = $method . '_' . $this->method . '_' . $this->type;
     } elseif ($this->method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->type)) {
         $fun = $method . '_' . $this->type;
     } elseif ($this->type == $this->restDefaultType && method_exists($this, $method . '_' . $this->method)) {
         $fun = $method . '_' . $this->method;
     }
     if (isset($fun)) {
         return App::invokeMethod([$this, $fun]);
     } else {
         // 抛出异常
         throw new \Exception('error action :' . $method);
     }
 }
Ejemplo n.º 3
0
 /**
  * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
  * @param string       $url          调用地址
  * @param string|array $vars         调用参数 支持字符串和数组
  * @param string       $layer        要调用的控制层名称
  * @param bool         $appendSuffix 是否添加类名后缀
  * @return mixed
  */
 public static function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
 {
     $info = pathinfo($url);
     $action = $info['basename'];
     $module = '.' != $info['dirname'] ? $info['dirname'] : Request::instance()->controller();
     $class = self::controller($module, $layer, $appendSuffix);
     if ($class) {
         if (is_scalar($vars)) {
             if (strpos($vars, '=')) {
                 parse_str($vars, $vars);
             } else {
                 $vars = [$vars];
             }
         }
         return App::invokeMethod([$class, $action . Config::get('action_suffix')], $vars);
     }
 }