一段低版本 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.
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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