23

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?

TaylorOtwell
  • 7,177
  • 7
  • 32
  • 42
  • This is one question that would be best answered by simply trying it. – erisco Aug 11 '10 at 13:49
  • 7
    I did try it and it *appeared* to work correctly. However, being relatively new to PHP, I wanted to make sure there wasn't any problem below the surface that might bite me later on. – TaylorOtwell Aug 11 '10 at 14:25
  • 15
    to think the creator of the most popular PHP framework was 'relatively new' to PHP only 5 years ago... Good Job @TaylorOtwell – dangel Feb 14 '16 at 04:48

6 Answers6

13

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.

Nima
  • 3,309
  • 6
  • 27
  • 44
selfawaresoup
  • 15,473
  • 7
  • 36
  • 47
  • So, are you saying that even if I include every file and dependency I need before using them, there can still be problems because of the order in which I include them? – TaylorOtwell Aug 11 '10 at 13:39
  • Yes, and keeping track of all your includes is pretty annoying. Autoloading can also improve your performance since it only loads what your script actually needs. Loading PHP files is usually an I/O-operation, which is mostly horribly slow. – selfawaresoup Aug 11 '10 at 16:27
  • And this is how composer was introduced to Laravel. – Phiter Nov 01 '18 at 11:31
  • Some libs like doctrine can be good to compile into one file: https://www.doctrine-project.org/projects/doctrine1/en/latest/manual/improving-performance.html – banankontakt Jun 03 '22 at 18:52
2

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.

tplaner
  • 8,363
  • 3
  • 31
  • 47
  • keeping everything in one file may seem comfortable for small projects but it prohibits you from reusing parts of the software and you have to load the entire stuff all the time, even if you don't actually use all of the classes/functions. It's quite a big waste of performance. – selfawaresoup Aug 11 '10 at 16:29
  • If you are using an autoloader, the files containing multiple classes need to have the classes ordered hierarchically. Otherwise you will have errors. – JG Estiot May 10 '15 at 02:55
0

Simply include them before they needed and everything will be fine.

fabrik
  • 14,094
  • 8
  • 55
  • 71
0

It would matter as if one class tried to use another which had not yet been parsed, then an error would occur. The best option is to place the classes in the script so the dependancy will still be intact.

Liam Spencer
  • 936
  • 1
  • 11
  • 25
-1

Yes it does matter and when a large number of classes are involved, it can cause problems. Using autoloading is not feasible when your production scripts needs to include 40 or 50 classes, which is always the case if your application is properly designed in accordance with best OOP practices, and you need to deal with forms etc...

Let me explain why the order matters. If you use an autoloader, every time the keyword extends is found, there is an attempt to autoload a class. So if you extend before the class being extended has been defined, it will trigger an autoload event.

When you develop your application, use a custom autoloader. Make that custom autoloader give you information about timing and memory used. It will load all class files one by one and waste a lot of time doing so. When your application is finished, you can optimize by concentrating many related classes into one file. For example, my form class is in class.form.php and it does not just load the form class. It loads another 50 classes related to forms. So I have everything in one file and when I create a new form(), the autoloader loads class.form.php which include the other classes that a form usually need (form_checkbox, form_input and so on) so when they are needed, they are not autoloader because they are already defined.

You can create a PHP script that will recurse, resolving dependencies and order all your classes within the single class file (class.form.php using my example) so that they all have the parent class defined before the extends keyword (which triggers autoloading) is found. I run php -w on the concatenated file which strips comments and spaces. It has reduced my form classes from 80K to 18K. Whenever I use a form, I create a new form() and it loads 18Ks worth of form-related classes, which is not excessive.

Initially I used the linux command cat but it does not work. You need a smarter script that can resolve the parent of each extended class and properly order classes in the file that contains many related classes.

JG Estiot
  • 969
  • 1
  • 10
  • 8
-1

If they all are in the same file, no the order shouldn't matter. There are some rare instances (I've seen with namespaces) where the order does matter, but don't worry about that too much unless you're pushing the envelope.

Try it. The worst that can happen is that it'll give you an error...

ircmaxell
  • 163,128
  • 34
  • 264
  • 314