I have a big system of classes and objects which rely on each other in order to function. Should I put all of the require_once() calls for dependencies at the top of each class file? ie:

    require_once( 'neededClass.php' );

    class MainClass{
        $this->neededClass = new neededClass();

Or should I make a big list of all my classes and include them at the base script?

2 Answers 2


Each file should require all its direct dependencies. If A calls B and C, then A requires both B and C, even if B also requires C. This keeps A from breaking if a new version of B does not require C.

  • Yep this is exactly how I landed on it. Nice to hear my brain is still making sense! Commented Aug 27, 2018 at 5:42

Consider using an autoloader to have PHP require files on demand. If you're already using Composer to load third party libraries, you can simply add your own autoloading configuration to the composer.json file.

  • Yeah since digging deeper I have seen that it makes a lot more sense to use namespaces and autoloading - thanks! Commented Mar 25, 2020 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.