I am using includes to pull in the various functions I am using, and I am now starting to use include to pull in chunks of HTML/PHP. Is there a point where I have overused includes?
I'd say it depends to what point your code is still readable. If someone not working on your project have difficulties to understand your code then yes, includes are overused.
If all you are using is includes then I would look into another way of doing it.
For example if you have a separate file for every function maybe look into putting them all in one file or putting them with similar functions.
As soon as you start having problems reading your own code that you wrote some time ago, it's definitely too much.
I recommend programming in object oriented PHP and using autoloaders to avoid include/require as far as possible. Excessive use of include/require often leads to unreadable and unmaintainable spaghetti code, which is very bad.
In small projects I usually just have one require statement to pull in my autoloader function(s) and in larger applications I use Zend Framework where I rely on Zend_Loader exclusively.
From a purist point of view I'd say: More than 3 includes/requires in your own code (without third party libs) is too much:
- One for inluding some iniitialization stuff
- One for loading the autoloader class/function
- And the one in the autoloader itself. There should only be one function that actually incudes/requires files. That function or method can then be reused in extended autoloader classes.
I mostly try to stick to that principle.
It's really a matter of architecture and optimisation. Rather than discuss what's the optimal number of includes per script, I'd advise using a template engine, e.g. Smarty because it allows you to:
- Separate markup from the program logic
- Use template tags and built-in functions to considerably ease the development
- Cache preprocessed PHP files making the whole thing a lot faster for your users
You can overuse anything but it's probably not doing you that much harm (just a few extra stats here and there). You have to remember that large projects like Drupal and Wordpress do hundreds, if not thousands of include
s.
If you're hooking in HTML, you might be getting a bit desperate. I'd personally have a good look at a proper templating language or even a framework that helped you into a MVC or MVT stance. It makes maintaining it a lot easier than chasing includes all over the place and (more importantly), keeps 95% of your logic out of your presentation files. Oh and they can maintain your databases in a much more programmatic modular method.
Basically Frameworks give you a lot of development benefits ;)
Symphony and CakePHP are both good frameworks but if you just want a look at templating, have a go with Smarty.