简体   繁体   English

PHP:Require()和类层次结构

[英]PHP: Require() and Class Hierarchies

I'm considering this issue largely a C++/C# programmer, which might help put things into perspective. 我主要在考虑这个问题,这是一个C ++ / C#程序员,这可能有助于将事情弄清楚。 I haven't been able to find a simple answer to my question, so I apologize if there's one readily available out there. 我无法找到问题的简单答案,因此我很抱歉是否有一个简单的答案。 This seems like it'd be a very common issue. 这似乎是一个非常普遍的问题。

Suppose I have class A , which is a container for instances of class B . 假设我有A类 ,它是B类实例的容器。 class B , in turn, is a container for instances of class C . B类又是C类实例的容器。 By container, I mean each class holds an array of certain kinds of objects and a few general parameters describing those objects. 通过容器,我的意思是每个类都包含一组特定类型的对象以及一些描述这些对象的常规参数。 Each class is stored in [className].php 每个类都存储在[className] .php中

Now, I have a script that needs to build an instance of class A. In order to do so, it will need to construct instances of class C. Each set of those will be used to build an instance of class B, and finally, all of the class Bs will be used to build an instance of class A. 现在,我有一个脚本,它需要构建类A的实例。为此,它将需要构造类C的实例。每组实例将用于构建类B的实例,最后,所有的B类将用于构建A类的实例。

The question is, in PHP, where is the best (least-error prone) place to put the require() statements? 问题是,在PHP中,放置require()语句的最佳位置(最容易出错)在哪里? Some ideas: 一些想法:

  • require() all three classes in the script, and nowhere else require()脚本中的所有三个类,无其他地方
  • require_once() the classes everywhere you need to use them. require_once()随处都可以使用它们的类。 In this case, you'd require_once() A, B, and C in the script, require B and C in A, and require_once() C in B. 在这种情况下,您需要在脚本中使用require_once()A,B和C,在A中需要B和C,在B中需要require_once()C。
  • require() only the class the next level down in the hierarchy. require()仅在层次结构中向下一级的类。 The script will require(A), A will require(B), and B will require(C). 脚本将需要(A),A将需要(B),B将需要(C)。 Thus, by requiring A in the script, you get access to all of the classes needed to populate A. 因此,通过在脚本中要求A,您可以访问填充A所需的所有类。
  • Something else, like autoloading. 还有其他东西,例如自动加载。

As always, I appreciate the ideas. 一如既往,我感谢这些想法。 While this might be subjective on some level, I do believe some ways may be objectively better than others, and that's what I'm after. 尽管这可能在某种程度上是主观的,但我确实相信某些方法可能客观上比其他方法更好,而这就是我所追求的。

Always use require_once to include classes. 始终使用require_once包含类。 Always . 总是 There is literally no downside. 字面上没有任何缺点。 Include the files via require_once in each file they're needed. 通过require_once将文件包括在所需的每个文件中。 This cannot fail: You cannot use require_once too many times, and the class is guaranteed to be included and available throughout the entire program. 这不会失败:您不能使用require_once太多次,并且可以保证在整个程序中都包含该类并且该类可用。

I'd go with autoloading. 我会自动加载。 You don't have to worry about where you require_once() your classes if it does it automatically for you. 如果它为您自动完成类,则不必担心您的类require_once() All you have to do is start using the classes you expect to be there, and PHP will call your autoloader to make sure that the classes are loaded. 您要做的就是开始使用您期望的类,PHP将调用您的自动加载器以确保已加载这些类。

If you are more used to programming in an environment that has you import things at the top of each file, you might be more comfortable using require_once() at the top of all of your files. 如果您更习惯于在每个文件的顶部都导入内容的环境中进行编程,则在所有文件的顶部使用require_once()可能会更舒适。 I almost prefer this over autoloading because it makes it very clear what each classes dependencies are. 与自动加载相比,我几乎更喜欢此方法,因为它可以很清楚地知道每个类的依赖关系是什么。 However, you get a lot of duplication which you might not enjoy. 但是,您会得到很多您可能不喜欢的重复信息。

Either way works. 无论哪种方式都行。 It is mostly a style choice. 它主要是样式选择。

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

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