tags:

views:

54

answers:

2

I currently have a AJAX heavy(almost everything) intranet webapp for a business. It is highly modularized(components and modules ala Joomla), with plenty of folders and files. ~ 80-100 different viewing pages (each very unique in it's own sense) on last count and will likely to increase in the near future.

I based around the design around commands and screens, the client request a command and sends the required data and receives the data that is displayed via javascript on the screen.

That said, there are generally two types of files, a display files with html, javascript, and a little php for templating. And also a php backend file with a single switch statement with actions such as, save, update and delete and maybe other function. Several pages/screens may use the same php backend.

Recently, I have been adding an server sided undo function that requires me to reuse some code. So, I took the chance to try out OOP but I notice that some functions are so simple, that creating a class, retrieving all the data then update all the related rows on the database seems like overkill for a simple action as speed is quite critical. Also I noticed there is only one class in an entire file.

So, what if the entire php is a class. So, between creating a class and methods, and using global variables and functions. Which is faster?

+1  A: 

The question is misguided. The problem is not one of speed, it's one of code organization. Using only global functions and variables, and lots of them, it'll get harder and harder to avoid naming conflicts and keep everything organized. Classes help you package and abstract things. Execution speed is a secondary concern, and in most cases won't increase noticeably, if at all. Development speed though can increase significantly over time, since you'll have to deal less with conflicts.

deceze
I agree that they do package and abstract things, and it does make it look simpler to look it that way but I noticed in the end, I am making 2-3 times of more SQL calls(typically in the constructor method). 1.5 times more code. So, far the best way is a mixed of classes and static methods
vener
@vener Then you're not using OOP correctly. Speaking purely code-wise, you can do the exact same thing as you do procedurally, with just the overhead that's necessary to manage objects (`class { }`, `$this`, `self`, `->`, `::`, `new`). Certainly it shouldn't increase the "actual work" you're doing. SQL queries don't magically multiply by wrapping them in objects. Proper OOP should actually decrease your LOC due to better reusability.
deceze
+1  A: 

Writing object oriented PHP will not affect your performance at all. If you use extensions like Zend Optimizer, it can even work faster.

So, there's really no reason not to use the much cleaner and more easily maintainable object oriented paradigm in PHP.

Writing purely procedural code may even lead to a performance drop since optimizations can be much harder and small details that eat up execution time are more likely to occur in such a messy environment.

Techpriester