Loading all PHP files all the time or only when they needed in Custom Plugin Development [closed]

It’s neither good or bad practice, the only true answer is it depends.

A concrete one size fits all answer to this question is impossible, and the question itself is micro-optimising.

  • For example if you know with certainty you don’t need to load a file then don’t load it. If that file contained hooks then they won’t be added.
  • if your file contains a class then why are you asking, just use an autoloader, and it’ll be auto-loaded the moment it’s first used
  • if your file contains functions then yes include it
  • most importantly, unless your file is a template file, loading it should do nothing and have no performance cost. e.g. loading a file with a class definition should define the class and nothing more, it should not execute code or create objects.

As for the performance implications, unless you have a huge number of PHP files this has negligible impact. When I say huge I mean PHP files that are in the double or triple digits of MB. Even then, it’s far more likely that database queries and HTTP calls have a greater impact on performance, and by orders of magnitude.

So unless you spot that performance is dropping due to disk activity, performance does not factor into this. And if it does, it’s probably because you’re not using a modern solid state drive on your server.