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?
In normal php functions, it doesn't matter. You can use both of the types. I like this answer, function definition must appear as a best practice it is like a tool that is ready to be called and you can call it later. Of course, it does not matter but for readability, function definition/declaration should go first.
Public Method/Function/Modifier/Keyword can be called outside of the class without any restriction as well as within the class access. Public Function/modifier should be accessed when the public function's programming code needed to execute the code instructions of it or else Public function will do nothing.
Definition and UsageThe new keyword is used to create an object from a class.
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.
Within 1 PHP file, you can declare functions/classes anywhere and call them anywhere (within that file), order does not matter.
As for includes, you must include the file BEFORE you attempt to use a declared function/class within that file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With