views:

909

answers:

6

I used to use procedural-style PHP. Later, I used to create some classes. Later, I learned Zend Framework and started to program in OOP style. Now my programs are based on my own framework (with elements of cms, but without any design in framework), which is built on the top of the Zend Framework.

Now it consists of lots classes. But the more I program, more I'm afraid. I'm afraid that my program will be slow because of them I'm afraid to add every another one class which can help me to develop but can slow the application.

All I know is that including lots of files slows application (using eAccelerator + gathering all the code in one file can speed up application 20 times!), but I have no idea if creaing new classes and objects slows PHP by itself.

Does anyone have any information about it?

A: 

Yes, every include makes your program slower, but there is more to it than that.

If you decompose your program, over many files, there is a point where you're including/parsing/executing the least amount of code, vs the overhead of including all those files.

Furthermore, having lots of files with little code ain't so bad, because, as you said, using things like eAccelerator, or APC, is a trivial way to get a crap ton of performance back. At the same time you get, if you believe in them, all the wonderful benefits of having and Object Oriented code base.

Also, slow on a per request basis != not scalable.

Updated

As requested, PHP is still faster at straight up array manipulation than it is classes. I vaguely remember the doctrine ORM project, and someone comparing hydration of arrays versus objects, and the arrays came out faster. It's not an order of magnitude, it is noticable, however -- this is in french, but the code and results are completely understandable.. Just a note, that doctrine uses magic methods __get, and __set a lot, and these are also slower than an explicit variable access, part of doctrine's object hydration slowness could be attributed to that, so I would treat it as a worst case scenario. Lastly, even if you're using arrays, if you have to do a lot of moving around in memory, or tonnes of tests, such as isset, or functions like 'in_array' (it's order N), you'll screw the performance benefits. Also remember that objects are just arrays underneath, the interpreter just treats them as a special. I would, personally, favour better code than a small performance increase, you'll get more benefit from having smarter algorithms.

Saem
Oh, thanks for the last line! It kills lots of my doubts :) What about the other lines, as I said, I know about include and asking about classes themself :)
valya
A: 

If you're using include_once() then you are causing an unnecessary slowdown, regardless of OOP design or not.

OOP will add an overhead to your code but I will bet that you will never notice it.

Ólafur Waage
Oh, thanks, I think I should remember it. Btw, I don't use includes very often. I prefer autoloading files then testing and gathering the bigger part of them in the one file then uploading to production
valya
+6  A: 

Here's good article discussing the issue. I also have seen some anecdotal bench-marks that will put OOP PHP overhead at 10-15% Personally I think OOP is better choice since at the end it may perform better just because it probably was better designed and thought true. Procedural code tends to be messy and hard to maintain. So at the end - it has to be how critical is performance difference for your app vs. ability to maintain, extend and simply comprehend

DroidIn.net
thanks! (btw, does the Script B in anecdotal bench-marking work? I think, where $basket should be passed by reference. Or I'm too C-lish?:)
valya
Imho, never choose one or the another with blinded eyes.If the script is small, and you need full performance, go with procedural, trying to write the less spaghetti that you can.If the script (not the project, i mean the script.. big projects have many small scripts, some in oop, some in procedural) and you need full maintainability, go with oop.
DaNieL
I agree and I'm (sorta) making the same point at the very end
DroidIn.net
A: 

You may reconsider to rethink your classes structure and how do you implement them. If you said that OOP is slower you may have to redesign your classes and how do you implement them. A class is just a template of an object, any bad designed method affects all the objects of that class.

Use inheritance and polimorfism the most you can, this will effectively reduce the amount of behaviors and independent methods your classes need, but first off all you need to create a good inheritance map, abstracting your first or mother classes as much as you can.

It is not a problem about how many classes do you have, the problem is how many methods, properties or fields they have and how well are those methods structured. Inheritance reduces the amount of methods to design drammatically and the amount of code to be compiled too.

backslash17
I haven't said that 'OOP is slower'. I've asked, if it is.
valya
A: 

Using large frameworks for web apps that actually do not require so large number of classes for everything is probably the worst problem that many are not aware of. Strip it down at least not to include every bit of code, keep just what you need and throw the rest.

Oliver
+3  A: 

The most important thing to remember is, design first, optimize later. A better design, which is more maintainable, is better than spaghetti code. Otherwise, you might as well write your web app in assembler. After you're done, you can profile (instead of guess), and optimize what seems slowest.

blockhead
profiling php is such a headache (for reading XDebug's files I have to use Linux. Or use the strange Air application which isn't show any info, but only convert the file into the other odd format. Or I can use Zend Debugger, but I don't really want to buy Zend Studio now)
valya
+1 for truthiness
Arms
Wincachegrind has been fine for reading profiler output under windows for me.
Frank Farmer
There is also webcachegrind, which is pretty interface to xdebug profile logs.
Saem