tags:

views:

894

answers:

6

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 (its 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")

+3  A: 

Multiple php includes is 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.

AlbertoPL
Yes, I realise that it's Aptana's fault that its crashing, I was just wondering whether there would be any negatives to splitting up my code into multiple files. It seems that it is actually better. Thanks.
Nico Burns
+1  A: 

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.

Draemon
A: 

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.

Eric Petroelje
A: 

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.

PatrikAkerstrand
A: 

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.

mgroves
well the advantage of using two levels would be that I could add a class to all my pages without changing them all. Which of course is the point of includes in the first place. Of course I could put all the classes in one file, but that was the whole point of the original question.
Nico Burns
What is the issue with include_once()? In my opinion a class should ensure that it includes all the classes it depends on. In that case, you need to include_once() to prevent multiple includes of the same class.
ctford
+1  A: 

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")

Pascal MARTIN
It would probably be about 5 classes, so 5 includes, at least to start with, and of course the advantage, as pointed out by several people above, is that I wouldn't have to include all of the classes in every page. Co-incidentally I have exactly 2 GB of RAM.
Nico Burns
to not include all of the files in every page, and not have to bother with include/require, you can have a look at autoloading (see http://php.net/autoload and http://php.net/manual/en/function.spl-autoload.php for instance) - the condition is you have to name your files correctly (you need to be able to guess the file's name and path from the class' name)
Pascal MARTIN
Ah, thanks for the link on autoloading, looks very useful. Does it have any downsides that you know about?
Nico Burns
Well, you have to name your directories/files in a coherent way (but that is not such a bad idea anyway ^^ ).Other than that, I don't really see downsides, on the contrary : one of the mains advantages is : only files and classes you need are loaded (better for speed and memory)
Pascal MARTIN