I have 5-10 files on one server that all catch XMLRPC-PHP requests. There's about 20 lines of require_once in each one that are all the same. It'd make life simpler for me to create a separate file that only has the requires and require that in my XMLRPC files. Is this best practice, or does it open up issues that I don't know about?

  • Have you considered consolidating the includes into a single file, perhaps as part of your build step? The performance gain can be worth it.
    – Jonno
    Commented Jul 24, 2012 at 15:49

3 Answers 3


I've done that before and it did save some effort, but got it fiddly as I didn't always need every single file and sometimes needed a few more. If you always need those and only those, then I think it's a good plan.

However, there are alternatives if you have control of the file hierarchy and are not using third party libraries e.g. using php's __autoload() function to have the files automatically included when you try to instantiate the class. I'd tend to prefer this if at all possible as it needs a lot less maintenance.


Basically any set of identical code in multiple places should be consolidated. As long as you're sure the exact same set of includes are required in each place I would put them together in one file. The performance difference (if any) is negligible and maintainability is more important.


Yes it would definitely make your life simpler. As mentioned by others before, I would recommend using the autoloading mechanism.

There is an implementation in the PHP manual, I found it pretty useful. It might look something like this:

function my_autoloader($class) {
    include 'classes/' . $class . '.class.php';


// Or, using an anonymous function as of PHP 5.3.0
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.class.php';

If you are on PHP 5.3, also consider using namespaces. It's actually even easier to have a good structure and a simple autoloader implemented by default:

spl_autoload_extensions(".php"); // comma-separated list

However, I remember a bug there. Usually I have a 'bootstrap' file, with this autoloader:

spl_autoload_register(function($class) {
    if(file_exists('./lib/' . str_replace('\\', '/', $class) . '.php')) {
        require_once './lib/' . str_replace('\\', '/', $class) . '.php';

Using this mechanism, you have a folder structure that mimics your namespace structure. With this approach, you have a good structure, and do not need to care about including your libraries and other classes.

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.