Does the order of class definition matter in PHP?

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.

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.

Leave a Comment