一段低版本 Facades

<?php

namespace App\Foundation\Facades;

abstract class BaseFacades
{
    /**
     * 你要具体调用的实例
     *
     * @return mixed 一个实例
     */
    abstract public function make();

    /**
     * Handle dynamic method calls into the model.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        $instance = $this->make();

        // return $this->make()->$method(...$parameter); // 如果你的 php 版本比较高,推荐使用这个
        return call_user_func_array([$instance, $method], $parameters);
    }

    /**
     * Handle dynamic static method calls into the method.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public static function __callStatic($method, $parameters)
    {
        $instance = new static;

        // return (new static)->$method(...$parameter); // 如果你的 php 版本比较高,推荐使用这个
        return call_user_func_array([$instance, $method], $parameters);
    }
}

使用

<?php

namespace App\Facades;

use App\Foundation\Facades\BaseFacades;
use App\Service\UserService;

class User extends BaseFacades
{
    public function make()
    {
        return new UserService();
    }
}
<?php

namespace App\Service;

class UserService
{
    public function all()
    {
        return 'User collection';
    }
}
<?php

namespace App\Controllers;

use App\Facades\User;

class UserController
{
    public function example()
    {
        return User::all();
    }
}

file

本作品采用《CC 协议》,转载必须注明作者和本文链接
Study hard and make progress every day. Study hard and make progress every day.
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!