2

Now assuming i have several classes all in one file and i run composer dump-autoload -o and i try to use any of those classes it works, now how about if all those classes were included into that single file using require_once, should that also work? I ask this in relation to a question i asked before which hasn't been answered yet: Loading external libraries in laravel-5

Now when i dump the contents(class) of each required file into that single file i am able to use those classes however when i use require_once i always get class not found? Is there a reason for this? Or am i simply not using it properly?

To further clarify what i am asking:

require_once("Class_1.php");
require_once("Class_2.php");
require_once("Class_3.php");
require_once("Class_4.php");

against this:

class Class_1 extends Class_Extender_1 {..}
class Class_2 extends Class_Extender_2 {..}
class Class_3 extends Class_Extender_3 {..}
class Class_4 extends Class_Extender_4 {..}
Community
  • 1
  • 1
user3718908x100
  • 7,939
  • 15
  • 64
  • 123

2 Answers2

1

You should avoid doing autoload optimization by hand.

I stated in this answer that classmap autoloading (which is also done when dumping the "optimized" autoloader) is not the fastest solution in every case, it depends on the code that gets executed. You have to measure if it accelerates anything, or you might make things worse without noticing.

The same applies to adding multiple classes into one file. The benefit of doing it is reduced disk I/O by accessing less files. The drawback is that more code gets executed without being needed in every case, and using more memory. Additionally it creates a less obvious code structure, so it's likely to also be a maintenance drawback as well.

Humans are usually bad at guessing what strategy is best - but they don't have to, they have machines that could do all this optimization themself. And this is what you should also try. Don't fiddle with optimized autoloading at the beginning - go for a good code structure, use PSR-4 autoloading (which requires one class per file, which is a good thing), and optimize something when the application is nearly done.

Community
  • 1
  • 1
Sven
  • 69,403
  • 10
  • 107
  • 109
  • Actually using psr-4 to autoload that file that requires the other files, each class extends some other class so i actually need all of them to be autoloaded. – user3718908x100 Apr 01 '15 at 23:08
  • But you need not do it yourself - that's what autoloading is about. And you probably will gain way more performance by optimizing one single database query than by optimizing the loading of classes in PHP. Remember: You cannot optimize something you cannot measure. Go ahead, use a profiler, identify the speed gain you get. If this is significant, apply it. If not - ignore for the time being. Get back to it later if necessary. – Sven Apr 01 '15 at 23:26
  • And one more thing, since we are talking about optimizing autoloading: Don't use the `*_once()` functions. Autoloading will only take place one time automatically, but these functions need to track which files have been loaded, which is having an influence on performance as well. – Sven Apr 01 '15 at 23:29
  • Thank you very much, i will look into all you have said. :) – user3718908x100 Apr 01 '15 at 23:33
0

Given a file src/require-everything.php with the following content

require_once("Class_1.php");
require_once("Class_2.php");
require_once("Class_3.php");
require_once("Class_4.php");

and a composer.json with the following content

{
    "autoload": {
        "files": ["src/require-everything.php"]
    }
}

the file src/require-everything.php will be autoloaded always, when you include Composer's vendor/autoload.php (after you dump the autoloader with Composer).

Please consider the fact, that this file is loaded on each request!

Its the same as placing a require 'src/require-everything.php'; in your application bootstrap.

My suggestion is to really use autoloading: either by wrapping the require part in a class itself or using classmap autoloading (on the folder with the classes).

{
    "autoload": {
        "classmap": ["src/folder-with-classes/"]
    }
}

The folder will be scanned for all .php files and all classnames will be extracted. This data forms the classmap with the relation of classname => filename. Now, if you use Class_1 in your app, Composers Autoloader will use the corresponding filename to load it automatically.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141