简体   繁体   English

to require_once()或Not to require_once()

[英]To require_once() or Not To require_once()

I am building a PHP CMS from the ground up. 我是从头开始构建PHP CMS的。 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: 考虑到负载require_once()放在服务器上以包含所有这些文件,以及用户必须等待页面加载的时间,我想知道我应该采取哪条路径:

  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!!! 感谢您的时间!!!

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. 例如,如果您开发CMS,事情可能会经常发生变化。 But if the CMS is installed on a server, that version does not change often. 但是,如果CMS安装在服务器上,则该版本不会经常更改。

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. 然后,您可以默认加载这些内容,以便不再为它们触发自动加载器。

Earlier this year, I came upon this exact problem while developing a framework in PHP. 今年早些时候,我在用PHP开发框架时遇到了这个问题。

I considered the pros-cons and here's my evaluation: 我考虑了利弊,这是我的评价:

Option 1 - Front Controller Pattern script include all other scripts 选项1 - Front Controller Pattern脚本包括所有其他脚本

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 . 我们有两个类RectangleShape Rectangle is a child class ie extension of Shape . Rectangle是一个子类,即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时,找不到Shape并且PHP将抛出错误。

Rectangle class: Rectangle类:

class Rectangle extends Shape{

}

Shape class: Shape类:

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 选项2 - 加载主包,然后根据需要加载其他包

Advantages 好处

  • Files are only included when needed. 仅在需要时包含文件。 Reduces overhead in another way 以另一种方式减少开销
  • Solves the problem mentioned in Option 1. 解决了选项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. 因此,为了使事情更具逻辑性并解决问题,我选择了选项2来寻找框架。

Don't load what you are not going to use. 不要加载你不会使用的东西。 Implement an autoloader or deepen your require_once's. 实施自动加载器或加深您的require_once。

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. 即使性能可忽略不计,包含较少的文件也会增加快速查找错误并确定应用程序流量的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM