【问题标题】:PHP Mess Detector avoid using static accessPHP Mess Detector 避免使用静态访问
【发布时间】:2020-08-15 13:11:09
【问题描述】:

我在我的 Laravel 控制器可调用类上使用 PHP Mess Detector,我收到这样的消息

Avoid using static access to class 'Illuminate\Support\Facades\Cache' in method '__invoke'

谁能解释一下为什么我必须避免在调用方法上使用静态访问?

【问题讨论】:

    标签: laravel controller phpmd illuminate


    【解决方案1】:

    静态访问导致对其他类的不可交换依赖 导致难以测试代码。不惜一切代价避免使用静态访问 而是通过构造函数注入依赖项。唯一的案例 什么时候可以接受静态访问是用于工厂方法。

    参考:https://phpmd.org/rules/cleancode.html#staticaccess

    【讨论】:

    • 好的..谢谢现在我知道为什么必须避免它
    猜你喜欢
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2016-09-25
    相关资源
    最近更新 更多