您的位置: 翼速应用 > 业内知识 > PHP框架 > 正文

解读用PHP特性trait实现简易Laravel Facade

简述

Facade 可以有效帮我实现方法的静态化。Laravel 大部分的扩展包都使用了 Facade。
下面的简易 Facade 主要是利用 PHP 的特性 trait,魔术方法 __callStatic,反射类 ReflectionClass

使用场景

后台系统大部分都会有类似这样的操作:

$user = User::find($id);if (!$user) {     throw new \Expection("资源不存在");}

这样似乎没有什么问题,但是还会存在下面这样的:


$article = Article::find($id);if (!$article) {
    throw new \Expection("资源不存在");}$article->delete();

这样写法十分不优雅。

上代码

1、首先我们应该要有一个 Service

<?phpnamespace App\Services;use App\Traits\ModeServiceTrait;class ModelService extends BaseService{
    use ModeServiceTrait;}

2、新建一个 Trait


trait 为了多继承而存在的,可以去 PHP官网 看文档。

<?php
namespace App\Traits;
use \ReflectionClass;
use \Exception;use \ReflectionException;
use Illuminate\Database\Eloquent\Model;
use App\Exceptions\ResourceException;
/**
 * @method static Model find(string $className, int $id, callable $callback = null)
 *
 * @see Model
 * @package App\Services
 */trait ModeServiceTrait{
    /**
     * 回调方法
     *
     * @param Model|null $model
     * @param string $method
     * @return Model
     * @throws ResourceException
     */
    public static function callback(Model|null $model, string $method): Model    {
        switch ($method)
        {
            case 'first':
            case 'find':
                if (!$model) {
                    throw new ResourceException("资源不存在");
                }
                break;

            default:

                break;
        }

        return $model;
    }

    /**
     * 调用不存在的方法时触发
     *
     * @param $method
     * @param $args
     * @return false|mixed
     * @throws ReflectionException
     * @throws ResourceException
     * @throws Exception
     */
    public static function __callStatic($method, $args)
    {
        $className = $args[0];
        $arg = $args[1];

        // 判断模型类是否存在
        if (!class_exists($className)) {
            throw new Exception("The class {$className} could not be found. from:" . __CLASS__);
        }

        // 利用反射实例化其类
        $reflection = new ReflectionClass($className);
        $instance = $reflection->newInstanceArgs();

        // 调用该不存在的方法
        $model = call_user_func_array([$instance, $method], [$arg]);

        // 如果存在复杂操作交给 callback
        return isset($args[2]) ? $args[2]($model) : self::callback($model, $method);
    }}

首先我们关注 __callStatic 这个魔术方法。 当调用不存在的静态方法时会触发该方法。和他相似的魔术方法是 __call。这是使用 __callStatic 是为了达到 Facade 的效果。

__callStatic 有两个回调参数 $method 是 被调用的且不存在的方法,$args $method 方法中所传递的参数(数组形式)。

这样一个简易的 trait 就完成了。

使用

我们新建一个 command

$ php artisan make:command TestCommand

写入下面的内容

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Services\ModelService;
use App\Models\Article\Article;
class TestCommand extends Command{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'a test';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $article = ModelService::find(Article::class, 1);

        $article = ModelService::find(Article::class, 1, function ($model) {
            return $model->load('author');
        });
    }}

其中的 Article 模型需要自己去创建。

接下来就可以看看效果了:

$ php artisan test:test

结语:这样我们就抛弃了使用 abstract 抽象类,来达到了跟 Facade 一样的效果。同时也做到了代码复用。

这样使用程序会多走很多步,但是跟优雅比起来,性能什么的都无所谓了。表达不是很清楚,需要自己深入体会了。 希望对技术大佬们有更深的知识点帮助。


我来说两句

0 条评论

推荐阅读

  • 响应式布局CSS媒体查询设备像素比介绍

    构建响应式网站布局最常见的是流体网格,灵活调整大小的站点布局技术,确保用户在使用的幕上获得完整的体验。响应式设计如何展示富媒体图像,可以通过以下几种方法。

    admin
  • 提升网站的性能快速加载的实用技巧

    网站速度很重要,快速加载的网站会带来更好的用户体验、更高的转化率、更多的参与度,而且在搜索引擎排名中也扮演重要角色,做SEO,网站硬件是起跑线,如果输在了起跑线,又怎么跟同行竞争。有许多方法可提升网站的性能,有一些技巧可以避免踩坑。

    admin
  • 织梦CMS TAG页找不到标签和实现彩色标签解决方法

    织梦cms是我们常见的网站程序系统的一款,在TAG标签中常常遇到的问题也很多。当我们点击 tags.php 页的某个标签的时候,有时会提示:“系统无此标签,可 能已经移除!” 但是我们检查程序后台,以及前台显示页面。这个标签确实存在,如果解决这个问题那?

    admin
  • HTML关于fieldset标签主要的作用

    在前端开发html页面中常用的标签很多,今天为大家带来的是关于HTML中fieldset标签主要的作用说明,根据技术分析HTML

    admin

精选专题