Laravel模型中 非静态方法 也能 静态调用

非静态方法 在 静态方法里调用

model 基类

class Model
{
    public function __call($method, $parameters)
    {
        if (in_array($method, ['increment', 'decrement'])) {
            return $this->$method(...$parameters);
        }
 
        return $this->forwardCallTo($this->newQuery(), $method, $parameters);
    }
 
    public static function __callStatic($method, $parameters)
    {
        return (new static)->$method(...$parameters);
    }
}

fadace 基类

illuminate/support/facades/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);
    }

记录你我
请先登录后发表评论
  • latest comments
  • 总共0条评论