tags:

views:

115

answers:

5

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?

+1  A: 

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.

Kaaviar
This is a good rule of thumb about your pages in general. I try write my code with this in mind, which is one reason I am trying to make a code a little more managable.
Brook Julias
A: 

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.

Sleepy Rhino
I separate my functions in three files at the moment-- a class file, a query builder file, and a db connect file. I'm just trying to find a way to make things a little simpler and it looks like using includes might not be the method I will use.
Brook Julias
i would agree with the use of OOP approach as per previous posters
Sleepy Rhino
+4  A: 

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.

Techpriester
I was just going to add that to my comment, but yes, OOP is what basically cuts off that high number of includes!
Ain
+1 for tipping about the Zend_Loader. also ot does not really stop the include function from being executed every time there's a file needed but it does help stop messy code and adds a more streamline system.
RobertPitt
@robertpitt: Sure it will still execute the include/require but I believe he maintainability limit is very much lower than any technical limitation to that.
Techpriester
A: 

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:

  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
Ain
I will have to look into a template engine. I think that might be the route I will have to take.
Brook Julias
+1  A: 

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 includes.

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.

Oli
Would these be similar to MODx?
Brook Julias
Not really. MODx is a whole content management system. A "framework" doesn't really care about what you do with it, just gives you guidelines and methods for how to do things. Just to blur things, most content management systems are built upon their own frameworks to allow people to extend them.
Oli
OK. Thanks for the clarification.
Brook Julias