require_once() 或不 require_once()

发布于 2024-11-15 08:00:43 字数 338 浏览 4 评论 0原文

我正在从头开始构建 PHP CMS。我的系统中有一个超级核心文件,目前我已自动导入构成系统核心的所有其他包和类。在典型的页面上,只使用其中的几个类和方法。

考虑到服务器上的加载 require_once() 包含所有这些文件,以及用户必须等待页面加载的时间,我想知道我应该采取哪条路径:

  1. 保留超级core 按原样,并自动包含包含此核心文件的每个页面的所有系统核心。
  2. 使用超级核心仅包含必要的包,例如数据库管理,并根据需要导入其他包/类。

有人可以让我知道这两个选项中哪一个最好,并简要概述其优缺点吗?

谢谢您的宝贵时间!

I am building a PHP CMS from the ground up. There is one super-core file within my system which I currently have automatically importing all other packages and classes that make up the core of the system. On a typical page, only a few of these classes and methods are used.

Considering the load require_once() puts on a server to include all of these files, and the time a user must wait for the page to load, I am wondering which path I should take:

  1. Keep the super-core as-is and automatically include all of the system core for each page that includes this core file.
  2. Use the super-core to include only essential packages, such as database management, and import additional packages/classes on an as-needed basis.

Could someone please let me know which of the two options are the best, as well as a brief overview of its pros and cons?

Thank you for your time!!!

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

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

发布评论

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

评论(3

烟沫凡尘 2024-11-22 08:00:43

您问的问题是最好的加载策略。这通常是与自动加载器相关的讨论

与任何策略一样,都有优点和缺点。包含所有文件可以让您免去忘记某个文件的麻烦。另一个自动加载器也不会忘记文件。

但是,您不能总是使用一种或另一种策略,但如果您实施多种策略,则可以根据需要进行选择。例如,如果您开发 CMS,事情可能会经常发生变化。但如果 CMS 安装在服务器上,则该版本不会经常更改。

因此,在生产中,将所有核心库合并到一个文件中并在启动时需要它们的策略可能会带来好处,具体取决于服务器的负载量。

为了以简单的方式构建自己的系统,我可以建议使用自动加载器。如果您按文件排列类文件,它们将在您使用该类时自动加载。

当您实现开发的某个步骤时,您实际上知道核心文件是什么或不是什么。然后,您可以默认加载它们,这样自动加载器就不会再为它们触发。

You're asking a question about which is the best load-strategy. This is often discussed related to auto-loaders.

As with any strategy, there are pros and cons. Including all files can save you the hassle to forget one. An autoloader on the other does not forget a file as well.

However you must not always use the one or other strategy but if you implement multiple you can choose as needed. For example if you develop your CMS things might change often. But if the CMS is installed on a server, that version does not change often.

So in production a strategy to combine all core libraries into one file and require them on startup can be a benefit depending on how much load a server has.

For an easy way to build own systems I can propose an autoloader. If you line-up your classes file by file they will get automatically loaded in the moment you use the class.

When you achieved a certain step in development you actually know what core files are or not. You can then load these by default so the autoloader would not be triggered any longer for them.

§对你不离不弃 2024-11-22 08:00:43

今年早些时候,我在开发 PHP 框架时遇到了这个问题。

我考虑了利弊,以下是我的评估:

选项 1 - 前端控制器模式脚本包括所有其他脚本

优点

  • 包的包含在一个脚本内完成;您一眼就能看出包含哪些文件、不包含哪些文件。
  • 包含特定包总是被调用一次,没有任何开销。

缺点

  • 考虑这样的情况:

我们有两个类 RectangleShapeRectangle 是一个子类,即Shape 的扩展。然而,核心脚本包括按字母顺序排列的类。因此,当包含Rectangle时,找不到Shape并且PHP将抛出错误。

Rectangle 类:

class Rectangle extends Shape{

}

Shape 类:

class Shape{

}
  • 当不需要的所有内容也加载到内存中时,会产生更多开销。

选项 2 - 加载主包,然后根据需要加载其他包

优点

  • 仅在需要时才包含文件。以另一种方式减少开销
  • 解决选项 1 中提到的问题。
  • 您可以专注于每个包需要从其他包获取什么,然后只需加载它们

缺点

  • 开销,因为可能会发生对特定包的多个请求。
  • 包包含在每个文件中完成。

编程代码是为人类服务的。因此,为了让事情更符合逻辑并分解问题,我选择了选项 2 来使用框架。

Earlier this year, I came upon this exact problem while developing a framework in PHP.

I considered the pros-cons and here's my evaluation:

Option 1 - Front Controller Pattern script include all other scripts

Advantages

  • Inclusion of packages are done within one script; you can see what files are included what are not at one glance.
  • Inclusion of a particular package is always called once, there is no overhead.

Disadvantages

  • Consider the case of such:

We have two classes Rectangle and Shape. Rectangle is a child class i.e. extension of Shape. However the core script includes the classes alphabetically. Thus when Rectangle is included, Shape is not found and PHP will throw an error.

Rectangle class:

class Rectangle extends Shape{

}

Shape class:

class Shape{

}
  • more overhead when everything that is not needed is also loaded into the memory.

Option 2 - Load main packages, then load other packages as-needed

Advantages

  • Files are only included when needed. Reduces overhead in another way
  • Solves the problem mentioned in Option 1.
  • You are able to concentrate on what each package requires from other packages and simply just load them

Disadvantages

  • Overhead as multiple requests for a particular package may occur.
  • Package inclusion is done in every single file.

Programming code is for human. Therefore to make things more logical and breaking down the problem, I chose option 2 to go for the framework.

又爬满兰若 2024-11-22 08:00:43

不要加载您不会使用的内容。实施自动加载器或加深您的require_once。

即使性能可以忽略不计,包含的文件越少也会提高您快速查找错误并确定应用程序流程的能力。

Don't load what you are not going to use. Implement an autoloader or deepen your require_once's.

Even if the performance is neglect-able, less files includes will increase your ability to quickly hunt down bugs and determine the flow of your application.

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