类定义的顺序在PHP中重要吗? [英] Does the order of class definition matter in PHP?

查看:69
本文介绍了类定义的顺序在PHP中重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将不同的php文件中包含多个类,并且将它们包含在一个文件中,并且其中一些类与其他类具有依赖关系,那么包含它们的顺序会很重要吗?或者,是在使用所有重要的东西之前简单地包括它们(以任何顺序)?

If I have several classes in different php files that I'm including into a single file, and some of these classes have dependencies on other classes, does the order in which they are included matter? Or, is simply including them (in any order) before they are used all that matters?

推荐答案

是的,很重要.在对象继承文档中,该注释作为注释被提及:

Yes, it matters. This is mentioned as a note in object inheritance documentation:

除非使用自动加载功能,否则必须先定义类,然后再使用它们.如果一个类扩展了另一个类,则必须在子类结构之前声明父类.此规则适用于继承其他类和接口的类.

Unless autoloading is used, then classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces.

我建议使用自动加载来解决类依赖关系.这样,仅在实际需要类时才加载类,而您不必手动添加每个PHP文件.

I recommend to use autoloading to resolve class dependencies. That way classes are only loaded when they are actually needed and you don't have to bother with including every single PHP file by hand.

尽管如此,您必须将每个类都保存在其自己的文件中.但这是保持所有物品整洁干净的好方法.

You have to keep every class in it's own file for this to work, though. But that's a good way to keep everything tidy and clean, anyway.

这篇关于类定义的顺序在PHP中重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆