Nico Burns
Nico Burns

Reputation: 17099

Is using multiple PHP includes a bad idea?

I'm in the process of creating a PHP site. It uses multiple PHP classes, which are currently all in one PHP include.

However, I am using Aptana IDE, and the file is now starting to crash it (it's around 400 lines). So I was wondering whether there would be any negative impact of including all the files seperately.

Current:

main file:

include("includes.php");

includes.php:

contains php classes

Suggested:

mainfile: main file:

include("includes.php");

includes.php:

include("class1.php");
include("class2.php")

Upvotes: 3

Views: 9954

Answers (7)

PaxBin
PaxBin

Reputation: 111

Multiple includes are the best way of well organasing your code, i recommend it as well, but in some cases, ( as mine) only the first include that gets executed i dont know why im stuck with it

Upvotes: 0

AlbertoPL
AlbertoPL

Reputation: 11509

Multiple PHP includes are fine, and 400 lines should not be a big deal. My concern would be with the Aptana IDE before I'd even consider my code to be the problem.

Breaking up your code into multiple PHP modules helps you to take a more object-oriented approach and simplifies your code base. I recommend it.

Upvotes: 6

Pascal MARTIN
Pascal MARTIN

Reputation: 401022

For just two files, the cost won't be too great ; for hundreds of files, it might be a bit more... But, then, another problem to consider is "how do I determine what goes into which file ?"

Nice answer for that is "one class per file" ; and, for those, "one directory per functionnal item"

You might want to consider using an opcode cache, if you can install extensions on your server ; for instance, I almost always work using APC (see also PHP manual), which is quite easy to install, and really good for performances (it can sometimes divide by 2 the CPU load of a server ^^ )

Just as a sidenote : if Aptana can't handle 400 lines files, you should really think about using another IDE ^^ (Eclipse PDT is not bad if you have 2 GB of RAM -- eclipse-based, like Aptana, so shouldn't be too "new")

Upvotes: 2

Matthew Groves
Matthew Groves

Reputation: 26151

I think your includes should generally only go one 'level' deep, unless you have a really good reason otherwise. What will happen is you will end up chasing down some issue and going on wild goose chases through include files, and you might even end up using stuff like "include_once" or "require_once", which is almost certainly a code smell.

Upvotes: 0

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

It's negative in the sence that it requires more disk I/O. However, in a production stage you should use opcode cache anyway, and this will negate much of the negative impact.

On the positive side, you will achieve a better code structure, where each class belongs to a single file. This makes testing easier, and also allows you to auto-load classes on demand, thus reading only the necessary files.

Upvotes: 0

Eric Petroelje
Eric Petroelje

Reputation: 60498

Personally, I like to include the files separately. If you include every class on every page, it just increases parsing overhead by processing lots of code that probably isn't even used on that page along with the associated overhead of reading the files from disk, etc.

Upvotes: 0

Draemon
Draemon

Reputation: 34711

An IDE crashing because of a 400 line file? I'd find a new IDE.

However, it is better to separate classes into separate files. Perhaps not strictly one class per file, but only closely related classes in the same file.

Upvotes: 3

Related Questions