Laravel 5.1 的 Facade 類 的 __callStatic 方法代碼如下:
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
switch (count($args)) {
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
case 2:
return $instance->$method($args[0], $args[1]);
case 3:
return $instance->$method($args[0], $args[1], $args[2]);
case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
default:
return call_user_func_array([$instance, $method], $args);
}
}
為什么不直接寫成:
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
return call_user_func_array([$instance, $method], $args);
}
8 回答

ibeautiful
TA貢獻1993條經(jīng)驗 獲得超6個贊
題主是不是看錯了或者看的是修改過的源碼,原始laravel中并沒有發(fā)現(xiàn)存在這些代碼,能否標(biāo)出具體的laravel版本和文件路徑
我看到的laravel的 Facade 類中代碼是這樣的
/**
* Handle dynamic, static calls to the object.
*
* @param string $method
* @param array $args
* @return mixed
*
* @throws \RuntimeException
*/
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
return $instance->$method(...$args);
}
- 8 回答
- 0 關(guān)注
- 645 瀏覽
添加回答
舉報
0/150
提交
取消