如何自动加载扩展类?
我打算使用 PHP 的自动加载功能来动态加载所需的类文件。现在,如果每个函数都有一个单独的文件,这可能会造成巨大的混乱,所以我希望并询问是否有一种方法可以让相关的类保留在 1 个类文件中并且仍然自动加载
function __autoload($class_name){
include('classes/' . $class_name . '.class.php');
}
假设有一个类名 Animals 和然后另一个类名为狗。狗类扩展了动物类,现在如果我调用狗类但不调用动物类,动物类文件是否仍会加载?
I am planning to use PHP's autoload function to dynamicly load only class files that are needed. Now this could create a huge mess if every single function has a seperate file, So I am hoping and asking is there a way to have related classes remain in 1 class file and still be auto-loaded
function __autoload($class_name){
include('classes/' . $class_name . '.class.php');
}
Let's say there is a class name animals and then another class named dogs. Dogs class extends animals class, now if I were to call the dogs class but NOT call the animals class, would the animals class file still be loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否考虑过对班级地点进行明确定义?有时将相关类分组很有意义。
这是一种经过验证的处理方法。
这段代码被放置在 auto_prepend_file 中(或首先包含),
并在引导文件中定义您的类以及它们所在的文件。
最后,通过调用启用 AutoLoad 的
好处之一是您可以定义“模块”:
然后在需要时显式加载它们:
最好的部分之一是您可以在模块中添加额外的
Import::Push
行,这将在以下位置定义其所有类运行时。Have you considered explicit definitions of your class locations? Sometimes it makes a lot of sense to group related classes.
Here is a proven way of handling it.
This code is placed in an
auto_prepend_file
(or included first)And in your bootstrap file, define your classes, and what file they are in.
And lastly, enable AutoLoad by calling
One of the nice things is that you can define "Modules":
And then load them explicitly when needed:
And one of the best parts is you can have additional
Import::Push
lines in the module, which will define all of its classes at runtime.是的。当您加载一个扩展另一个类的类时,PHP 必须加载基类,以便它知道它扩展了什么。
回复:每个文件存储多个类的想法:这不适用于您提供的自动加载功能。每个文件一个类确实是最佳实践,特别是对于自动加载的类。
如果一个文件中有多个类,那么您确实不应该尝试从该文件自动加载任何类。
Yes. When you load an class that extends another class, PHP must load the base class so it knows what it's extending.
re: the idea of storing multiple classes per file: This will not work with the autoload function you provided. One class per file is really the best practice, especially for autoloaded classes.
If you have more than one class in a file, you really shouldn't attempt to autoload any classes from that file.
是的,除非您不包含/不需要类文件,否则它将加载。
您必须始终导入包含所需 PHP 代码的所有文件。 PHP 本身无法猜测您为类文件指定的名称。例如,与 Java 不同,PHP 对类没有任何文件命名要求。
解决这个问题的常见方法是将相关的类分组在一个文件中。创建一个“模块”。
Yes it will load unless you don't include/require the class file.
You have to always import any files that contain needed PHP code. PHP itself can't guess the name you gave to your class file. Unlike Java for instance, PHP doesn't have any file naming requirements for classes.
The common way so solve this problem is to group related classes in one single file. Creating a "module".
有一个解决方法,我刚刚使用过它并且似乎有效:
假设我们有一个名为animals 的自动加载类,存储在animals.class.php 中。在同一个文件中,你可以有其他扩展动物的类
现在...当你需要使用你的类
dogs
时,你需要 PHP 自动加载它的父类(这样做你确信它也会解析同一文件中的所有扩展类),因此您只需编写:There's a workaround, I've just used it and it seems to work:
Let's assume we have an autoloaded class called animals stored in animals.class.php. In the same file you could have other classes that extends animals
Now... when you need to use your class
dogs
you need PHP autoload it's parent class (and doing this you're sure it will parse also all the extendend classes inside the same file) so you just have to write: