简体   繁体   中英

Is there such a thing as an over use of PHP's include()?

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?

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:

  1. One for inluding some iniitialization stuff
  2. One for loading the autoloader class/function
  3. 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.

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.

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 .

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.

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, eg Smarty because it allows you to:

  1. Separate markup from the program logic
  2. Use template tags and built-in functions to considerably ease the development
  3. Cache preprocessed PHP files making the whole thing a lot faster for your users

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM