require_once() 或不 require_once()
我正在从头开始构建 PHP CMS。我的系统中有一个超级核心文件,目前我已自动导入构成系统核心的所有其他包和类。在典型的页面上,只使用其中的几个类和方法。
考虑到服务器上的加载 require_once() 包含所有这些文件,以及用户必须等待页面加载的时间,我想知道我应该采取哪条路径:
- 保留超级core 按原样,并自动包含包含此核心文件的每个页面的所有系统核心。
- 使用超级核心仅包含必要的包,例如数据库管理,并根据需要导入其他包/类。
有人可以让我知道这两个选项中哪一个最好,并简要概述其优缺点吗?
谢谢您的宝贵时间!
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:
- Keep the super-core as-is and automatically include all of the system core for each page that includes this core file.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您问的问题是最好的加载策略。这通常是与自动加载器相关的讨论。
与任何策略一样,都有优点和缺点。包含所有文件可以让您免去忘记某个文件的麻烦。另一个自动加载器也不会忘记文件。
但是,您不能总是使用一种或另一种策略,但如果您实施多种策略,则可以根据需要进行选择。例如,如果您开发 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.
今年早些时候,我在开发 PHP 框架时遇到了这个问题。
我考虑了利弊,以下是我的评估:
选项 1 - 前端控制器模式脚本包括所有其他脚本
优点
缺点
我们有两个类
Rectangle
和Shape
。Rectangle
是一个子类,即Shape
的扩展。然而,核心脚本包括按字母顺序排列的类。因此,当包含Rectangle
时,找不到Shape
并且PHP将抛出错误。Rectangle
类:Shape
类:选项 2 - 加载主包,然后根据需要加载其他包
优点
缺点
编程代码是为人类服务的。因此,为了让事情更符合逻辑并分解问题,我选择了选项 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
Disadvantages
We have two classes
Rectangle
andShape
.Rectangle
is a child class i.e. extension ofShape
. However the core script includes the classes alphabetically. Thus whenRectangle
is included,Shape
is not found and PHP will throw an error.Rectangle
class:Shape
class:Option 2 - Load main packages, then load other packages as-needed
Advantages
Disadvantages
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.
不要加载您不会使用的内容。实施自动加载器或加深您的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.