think\Hook::exec PHP Method

exec() public static method

执行某个行为
public static exec ( mixed $class, string $tag = '', Mixed &$params = null, mixed $extra = null ) : mixed
$class mixed 要执行的行为
$tag string 方法名(标签名)
$params Mixed 传人的参数
$extra mixed 额外参数
return mixed
    public static function exec($class, $tag = '', &$params = null, $extra = null)
    {
        App::$debug && Debug::remark('behavior_start', 'time');
        if (is_callable($class)) {
            $result = call_user_func_array($class, [&$params, $extra]);
            $class = 'Closure';
        } elseif (is_object($class)) {
            $result = call_user_func_array([$class, $tag], [&$params, $extra]);
            $class = get_class($class);
        } else {
            $obj = new $class();
            $result = $tag && is_callable([$obj, $tag]) ? $obj->{$tag}($params, $extra) : $obj->run($params, $extra);
        }
        if (App::$debug) {
            Debug::remark('behavior_end', 'time');
            Log::record('[ BEHAVIOR ] Run ' . $class . ' @' . $tag . ' [ RunTime:' . Debug::getRangeTime('behavior_start', 'behavior_end') . 's ]', 'info');
        }
        return $result;
    }

Usage Example

Example #1
0
 public function testExec()
 {
     $data['id'] = 0;
     $data['name'] = 'thinkphp';
     $this->assertEquals(true, Hook::exec('\\tests\\thinkphp\\library\\think\\behavior\\One'));
     $this->assertEquals(false, Hook::exec('\\tests\\thinkphp\\library\\think\\behavior\\One', 'test', $data));
     $this->assertEquals('test', $data['name']);
     $this->assertEquals('Closure', Hook::exec(function (&$data) {
         $data['name'] = 'Closure';
         return 'Closure';
     }));
 }
All Usage Examples Of think\Hook::exec