Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is object-oriented PHP slow?

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 creating new classes and objects slows PHP by itself.

Does anyone have any information about it?

like image 599
Valentin Golev Avatar asked Oct 30 '09 19:10

Valentin Golev


People also ask

Is Object Oriented Programming slow?

Larger program size: Object-oriented programs typically involve more lines of code than procedural programs. 3. Slower programs: Object-oriented programs are typically slower than procedure- based programs, as they typically require more instructions to be executed.

Is PHP OOP hard?

Learning PHP OOP from procedural programming is a bit tricky for me. I almost gave up but the demand for reusability kept me going. The concept of class and object seemed difficult for me, but after many attempts and approaches, I was able to get the concepts and the implementation.

Is PHP functional or OOP?

Yes, the latest versions of PHP are object oriented. That is, you can write classes yourself, use inheritance, and where appropriate, the built in functionality is built in objects too (like MySQL features).

Is Oops worth?

Yes, it is almost always a good idea to use OOP. This is because OOP is a style of coding, and coding styles for the most part are easily able to be transferred accross languages. People don't use coding-styles because they use a certain language.


2 Answers

This bugs me. See...procedural code is not always spaghetti code, yet the OOP fanboys always presume that it is. I've written several procedural based web apps as well as an IRC services daemon in PHP. Amazingly, it seems to outperform most of the other ones that are out there and editing it is super easy. One of my friends who generally does OOP took a look at it and said "no code has the right to be this clean"

Conversely, I wrote my own PHP framework (out of boredom) and it was done in a purely OOP manner.

A good programmer can write great procedural code without the overhead classes bring. A bad programmer who uses OOP will always write crappy OOP code that slows things down.

There is no one right answer to which is better for PHP, but rather which is better for the exact scenario.

like image 138
Jeremy Avatar answered Sep 17 '22 17:09

Jeremy


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

like image 38
Bostone Avatar answered Sep 19 '22 17:09

Bostone