有条件地将类文件包含在 PHP 中另一个类的方法中有何优点和缺点?

发布于 2024-09-20 00:41:26 字数 1024 浏览 3 评论 0原文

这个问题 关于将所有类包含在需要所有类的解释器的目录中,建议处理该问题的更好方法是有条件地仅包含解释器本次迭代所需的命令文件。例如,而不是这样:

require_once('CommandA.php');
require_once('CommandB.php');
require_once('CommandC.php');

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                $c = new A();
                $c->execute();
                break;
        }
    }
}

做更多类似这样的事情:

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                require_once('CommandA.php');
                $c = new A();
                $c->execute();
                break;
        }
    }
}

使用第二个选择的优点和缺点是什么 - 动态且有条件地仅加载所需的命令 - 与第一个选择相反 - 包括所有文件在文件的头部,C 风格?

在我之前的问题的答案中,速度、占用空间和易于编写被列为条件加载的优点。在解决此问题的其他问题中,我看到它建议 c 样式加载更具可读性和可维护性。谁能详细解释一下这些吗?还有其他优点或缺点吗?

In this question about including all the classes in a directory for an interpreter that needs all of them, it was suggested that a better way to handle the problem would be to conditionally include only the Command file that is needed for this iteration of the interpreter. For example, instead of this:

require_once('CommandA.php');
require_once('CommandB.php');
require_once('CommandC.php');

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                $c = new A();
                $c->execute();
                break;
        }
    }
}

Do something more like this:

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                require_once('CommandA.php');
                $c = new A();
                $c->execute();
                break;
        }
    }
}

What are the advantages and disadvantages of going with the second choice -- dynamically and conditionally loading only the needed command -- as opposed to the first one -- including all of the files at the head of the file, C-style?

In the answer given to my previous question, speed, footprint and ease of writing were listed as advantages of conditionally loading. In other questions where this is addressed, I've seen it suggested that c-style loading is more readable and maintainable. Can anyone elaborate on these? Any other advantages or disadvantages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2024-09-27 00:41:30

条件加载的主要优点是,每次加载该类时,它不会包含整个 php 文件树。当您使用代码缓存时(您应该这样做),每次虚拟机遇到 /(include|require)(once)?/ 时都会检查文件是否有更新,因此会包含不必要的 php 文件减慢你的代码速度。

这种特殊方法的缺点是难以维护,并且容易出错。将一件事写两次会产生代码味道。编写My_Class意味着您需要这个类,编写include“My_Class.php”也意味着这个。这是不对的。

我建议使用自动加载: http://php.net/manual/en/language .oop5.autoload.php

The main advantage of the conditional loading is that it won't include a whole tree of php files every time you load that class. Event when you use a code cache (and you should), the files are checked for updates every time the vm encounters an /(include|require)(once)?/, so including unnecessary php files will slow down your code.

The disadvantages of this particular method is, that it is harder to maintain, and easier to make mistakes. Writing one thing twice is a code smell. Wirting My_Class means, that you need this class, and writing include "My_Class.php" also means this. This is not right.

I suggest using autoloading: http://php.net/manual/en/language.oop5.autoload.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文