performance implications of classes in their own file

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

performance implications of classes in their own file

Post by Luke »

What are the performance implications of putting classes in their own file and having to load as many files as you have classes? Seems like it would be a high cost to performance... am I wrong?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As I've said before, it can vary quite a bit depending on your particular situation. In general, it is considered a better practice of storing classes in separate files. OOP isn't about performance.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

There is a performance overhead, but the time you would save with this optimization means you would incur a maintainability cost which is just not worth it. Only when your project is working should you look at the effeciency of your code and refactor as neccessary if something is unacceptably slow.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

There is a performance overhead when using classes no matter what in PHP.

Here is the funny thing...

Loading a bunch of small classes from files is usually faster than loading one large class that has all of the came class functions as the bunch of small classes.

The larger the class the longer it takes to create the object. That is a firm rule for PHP.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Look, if you're doing anything remotely interesting, userspace code execution will usually outweigh parse time.

If the extra performance really is necessary, install an opcode cache or create a utility script that inlines all includes before deployment (not that difficult with PHP's parsing functions). The readability benefit from seperate pages is just too much to trade off for this argument.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK :D
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Sure an OpCode cache is a good idea but not every server has something like that. Also, the performance gain when creating a new object isn't that much when using an OpCode cache. It's just the way PHP creates and manages objects.

Creating big class objects with PHP (any version) is going to be slow even using a precompiler. You would be better off creating a group of SMALL class objects as they are needed. The shotgun, large class method is not a good idea with PHP.
Post Reply