Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
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 Jul 24 '12 at 15:49
add comment

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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';
}

spl_autoload_register('my_autoloader');

// 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
spl_autoload_register();

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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