Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Feb 1, 2010, 10:10   #1
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
Post OOP PHP Inefficient?

I'm revisiting an older thread (PHP OOP makes slow application? [4037709]) on this subject (on the recommendation by this site due to its age being 14 months old) in order to shed some light on the subject of using an interpretive language in an Object Oriented fashion to render a stateless Web page. That’s three ways to slow down a Web server.

Compiled OOP languages, like C++ and Java, avoid much of their runtime inefficiencies by shifting that burden to compile time. Web pages written in JSP (Java Server Pages), for example, run fairly efficiently despite their OOP nature.

Then you take the nature of a stateless Web browser application. The Web server must render that same page every time it’s visited. The Web application must keep track of session information in order to maintain a seamless user experience. Techniques for storing and retrieving objects in a PHP session don’t appear to be any more efficient that of rebuilding them from a relational database. Since there’s no native way of storing session objects in PHP, methods for serializing them are employed to store them in a session that is ultimately stored on the server’s disk space. That adds complexity for storing, retrieving, and rebuilding the object later on that duplicates a simple database query. It also adds a level on inconsistency with the database if it is out-of-sync with the stored session object at time of retrieval (albeit maybe a desired feature; an object frozen in session time, for example).

My interest in this debate arises from an effort to teach myself PHP, MySQL, and Apache Web development using Joomla CMS. Having arrived at the point of mandatory source code modification, I’m using the Eclipse PDT to step through code and find that Joomla is quite object oriented. Using the Web server running on my ancient development machine (don’t laugh: P4 2.66GHz, 1G RAM), I can definitely tell where in the Joomla code my machine is struggling.

Joomla already in it infancy of version 1.5.15 has a reputation of being slow. While on the other hand, I come across discussions in forums about how one can increase speed microseconds by using one PHP primitive over another (obviously, not the OOP crowd).

In the earlier thread, there’s discussion about how OOP programmers observe rules of copious commenting in order for OO code to be re-used, and that the lack there of would defeat the purpose of choosing an object oriented approach to programming. Well, here’s an example from the base index.php page of Joomla:

/**
* CREATE THE APPLICATION
*
* NOTE :
*/
$mainframe =& JFactory::getApplication('site');

/**
* INITIALISE THE APPLICATION
*
* NOTE :
*/
// set the language
$mainframe->initialise();

/**
* ROUTE THE APPLICATION
*
* NOTE :
*/
$mainframe->route();

/**
* DISPATCH THE APPLICATION
*
* NOTE :
*/
$option = JRequest::getCmd('option');
$mainframe->dispatch($option);

/**
* RENDER THE APPLICATION
*
* NOTE :
*/

$mainframe->render();

Amazing! I couldn’t tell from the sparse comments that $mainframe->dispatch() was going to dispatch the application, especially not from the empty Note: comments. It would be nice to know what this student meant exactly by dispatch. Maybe he had intended to get around to filling in that comment fields after finishing his midterms. Or it simply could be that this student is suffering from college-on-the-brain and assumes that anyone worth his salt would obviously know what is meant by dispatch because he would’ve taken similar courses. I must be a lesser programmer, because I had to actually step through this entire author’s OOP PHP code to determine the difference between create, initialize, route, dispatch, and render.

In my search for vindication, I came across documentation that seems to justify my argument against using interpreted OOP in a Web-based application. Joomla’s nearest competitor, Drupal, appears to avoid OOP PHP and is significantly faster:

(Unfortunately, this site is not letting me post URLs, but this was taken from wiki's drupal page.)
...Drupal's Web pages were delivered "significantly faster" compared with Joomla.

Without personal experience using Drupal, I take it from this article that it avoids OOP:

(...and this from Drupal's own site.)

Drupal programming from an object-oriented perspective
Drupal often gets criticized by newcomers who believe that object-oriented programming (OOP) is always the best way to design software architecture, and since they do not see the word "class" in the Drupal code, it must be inferior to other solutions. In fact, it is true that Drupal does not use many of the OOP features of PHP, but it is a mistake to think that the use of classes is synonymous with object-oriented design. This article will cover several of the features of Drupal from an object-oriented perspective, so programmers comfortable with that paradigm can begin to feel at home in the Drupal code base, and hopefully be able to choose the right tool for the job.

The point I’m trying to make is that when choosing the tools to do the job, to avoid the performance pitfall that software giants like Microsoft tend to fall into where they sacrifice software performance because computers always get faster. It took my using decade-old hardware as a development server to realize this application I’m developing wont to scale very well. Is that an acceptable tradeoff for OOP?

Please share your thoughts, opinions, and observations.
PalaDolphin is offline   Reply With Quote
Old Feb 1, 2010, 10:23   #2
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
You started talking about objects then moved on to sessions. Huh? This is completely irrelevant because the object's class definition still needs parsed.

Anyway...

The Drupal code you showed above is indeed crap. Most open-source projects are actually (Drupal, WordPress, SMF, ...). The comments in that code are NOT what a good programmer would write. If I was editing the code, I would remove every single one of those comments because they are bloat and unnecessary.

But that still has nothing to do with OOP or performance.

To improve performance for any PHP code, you should use something like APC which caches the file's byte-code, skipping re-compilation of the PHP files. This is used on most (all?) big websites and gives a significant performance boost.

Things Microsoft do are generally not web-based, and a lot of Windows is written in ancient languages without OOP support.

So, basically, don't worry about performance . Use objects like crazy.
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 1, 2010, 11:00   #3
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
Quote:
The Drupal code you showed above is indeed crap.
The code I inserted was from Joomla, not Drupal.

Thanks for the APC info.
PalaDolphin is offline   Reply With Quote
Old Feb 1, 2010, 11:24   #4
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
Quote:
Originally Posted by PalaDolphin View Post
The code I inserted was from Joomla, not Drupal.

Thanks for the APC info.
Oops! My bad.
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 2, 2010, 13:11   #5
SituationSoap
SitePoint Addict
 
Join Date: Apr 2009
Posts: 248
I hear making judgments on decades-old programming paradigms based on issues with a poorly written framework running on ancient hardware and a non-server (and uncontrolled) OS is a great idea.

If you're honestly interested in learning why you're wrong, I'd be more than happy to pull your post apart, but this really just smells like a very elaborate troll, to me.
SituationSoap is offline   Reply With Quote
Old Feb 2, 2010, 13:42   #6
pmw57
Unobtrusively zen
bronze trophy
 
pmw57's Avatar
 
Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
I suggest that people give the book Clean Code a good read. Your local library should have it too.
Even though the book uses Java for its code samples (not JavaScript, but big daddy Java) there is a lot of good advice in there that translates well across multiple languages.
__________________
Javascript Reference, Quirksmode
HTML Validation, JSLint

Paul Wilkins
pmw57 is online now   Reply With Quote
Old Feb 2, 2010, 16:45   #7
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
Question

This is a serious post SituationSoap. Pull apart at will.

I'm all for OOP programming. I've programed in C++ and Java, worked on sites developing JSP and servlets, and fully appreciate the advantages of OOP. Originally an assembly and C programmer, I learned OOP through C++, and later, Java.

I've learned the fundamentals of PHP. I really want to know why I am attempting to learn OOP in PHP and where inefficiencies lie.

So, let me ask this: Is there pre-compiling in PHP to reduce overhead?
PalaDolphin is offline   Reply With Quote
Old Feb 2, 2010, 16:49   #8
pmw57
Unobtrusively zen
bronze trophy
 
pmw57's Avatar
 
Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
Not as we understand it, but Facebook have just recently created their own.
http://developers.facebook.com/news....og=1&story=358

I suspect that the push to use OOP in PHP is that it can help to remove complexity from code development and management.

Working with big-ball-of-mud procedural code can easily end up costing more in development and testing time than when using OOP techniques.
__________________
Javascript Reference, Quirksmode
HTML Validation, JSLint

Paul Wilkins
pmw57 is online now   Reply With Quote
Old Feb 2, 2010, 17:09   #9
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
pmw57,
This HipHop for PHP was released this morning? Converting PHP into C++ sounds intriguing. I'd like to play with it. YACC!
PalaDolphin is offline   Reply With Quote
Old Feb 2, 2010, 17:10   #10
pmw57
Unobtrusively zen
bronze trophy
 
pmw57's Avatar
 
Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
Yep, this morning - my news wire has been running hot.
__________________
Javascript Reference, Quirksmode
HTML Validation, JSLint

Paul Wilkins
pmw57 is online now   Reply With Quote
Old Feb 3, 2010, 02:05   #11
kyberfabrikken
Community Advisor
silver trophy
 
kyberfabrikken's Avatar
 
Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 6,067
Quote:
Originally Posted by PalaDolphin View Post
I really want to know why I am attempting to learn OOP in PHP and where inefficiencies lie.
Using OOP in PHP is, generally speaking, not a performance hazard. Joomla is slow, not because it uses objects, but because it's crap.

Quote:
Originally Posted by PalaDolphin View Post
So, let me ask this: Is there pre-compiling in PHP to reduce overhead?
The PHP run time consists of two steps; A parser that converts sourcecode to bytecode (opcodes), and an interpreter (virtual machine), which can execute the bytecode. Normally, the opcodes are thrown away between each request, but if you use an opcode cache, the opcodes are stored and reused between requests. This can typically increase performance by 30%.

However, it's rather important to realise that most PHP applications aren't CPU bound. They typically wait for external components, such as a database or the file system. No matter how fast the PHP code is executed, this won't go any faster.

If you're actually interested in performance, I would recommend that you install Xdebug and use its profiling capabilities. You can then use a tool like WinCacheGrind or KCacheGrind to inspect the profiling data.
kyberfabrikken is offline   Reply With Quote
Old Feb 3, 2010, 02:16   #12
JREAM
John 8:24
 
JREAM's Avatar
 
Join Date: Sep 2007
Location: Florida
Posts: 1,304
OOP PHP changed my life. Profile your code, Try some Singleton and Registry Patterns too.
JREAM is offline   Reply With Quote
Old Feb 3, 2010, 06:05   #13
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
kyberfabrikken,
Valuable information regarding the innards of PHP. It's good to know there are many options for speeding up PHP. This also points to a need for efficiency. It's good to know one can code OOP PHP with impunity.
PalaDolphin is offline   Reply With Quote
Old Feb 3, 2010, 13:15   #14
SituationSoap
SitePoint Addict
 
Join Date: Apr 2009
Posts: 248
I was going to come back and dissect your OP, but it sounds like everyone here has it covered. Basically, it boils down to: don't assume things based on limited experience.
SituationSoap is offline   Reply With Quote
Old Feb 3, 2010, 14:00   #15
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
...what I have to offer.

Isn't this an area for discussion? To learn from other. There is so much I don't know about PHP. Just because it is similar to C and C++ doesn't mean it acts the same way.
PalaDolphin is offline   Reply With Quote
Old Feb 3, 2010, 16:11   #16
nice123
SitePoint Member
 
Join Date: Jan 2010
Posts: 13
OK, OOP PHP script is slower than non OOP PHP script doing the same thing. But it does not mean we have to abandon OOP.
nice123 is offline   Reply With Quote
Old Feb 3, 2010, 16:18   #17
pmw57
Unobtrusively zen
bronze trophy
 
pmw57's Avatar
 
Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
Let's equate this to something else for comparison.

When constucting a building, there is invariably some kind of building code that needs to be adhered to.

When someone rushes in ignoring the building code the problems may not show up right away, but the home will start to show signs of the weaknesses at some later stage.

OOP is equivalent to following the building codes. It may be slower and sometimes more expensive, but the positive aspects far outweigh the negatives.
__________________
Javascript Reference, Quirksmode
HTML Validation, JSLint

Paul Wilkins
pmw57 is online now   Reply With Quote
Old Feb 3, 2010, 19:12   #18
Sagewing
Life is short. Be happy today!
silver trophybronze trophy
SitePoint Award Recipient
 
Sagewing's Avatar
 
Join Date: Apr 2003
Location: Los Angeles, Bangkok, Manhattan, Louisiana
Posts: 3,747
Quote:
Originally Posted by pmw57 View Post
Let's equate this to something else for comparison.

When constucting a building, there is invariably some kind of building code that needs to be adhered to.

When someone rushes in ignoring the building code the problems may not show up right away, but the home will start to show signs of the weaknesses at some later stage.

OOP is equivalent to following the building codes. It may be slower and sometimes more expensive, but the positive aspects far outweigh the negatives.
I'm not so sure about that analogy. Building codes tend to deal with the strength of the materials used, the amount of structural support that is in place, the flammability and toxicity of the materials to be used, etc.

A code framework/method deals more with the style of architecture that the code will be built in.

Say I built a 5 story building. I could build it out of brick and have it 100% up to code. Or, I could built it with metal framing and girder and have it 100% up to code. Both approaches could produce a building that could look identical (with some siding!) , be equally safe, and just fine.

But one building might be easier to maintain, more efficient, more expandable, etc.

Then again, if you are building a shed and you do some complex framing and foundation, you might be wasting your time.
__________________
Please support Children International

The fewer our wants, the nearer we resemble the gods. — Socrates
Sagewing is offline   Reply With Quote
Old Feb 3, 2010, 19:19   #19
pmw57
Unobtrusively zen
bronze trophy
 
pmw57's Avatar
 
Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
Quote:
Originally Posted by Sagewing View Post
Then again, if you are building a shed and you do some complex framing and foundation, you might be wasting your time.
Good point. A nice example of when to not use them is this Hello world! using design patterns
__________________
Javascript Reference, Quirksmode
HTML Validation, JSLint

Paul Wilkins
pmw57 is online now   Reply With Quote
Old Feb 4, 2010, 10:07   #20
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
Quote:
OOP is equivalent to following the building codes. It may be slower and sometimes more expensive, but the positive aspects far outweigh the negatives.
Yes, but isn't OOP code w/o comments academic masturbation? Joomla code is my introduction to OOP PHP and it has nothing in the way of comments. It's open source, so check for yourself: Joomla version 1.5.15. It's like constructing a building with no blueprints.
PalaDolphin is offline   Reply With Quote
Old Feb 4, 2010, 10:30   #21
cholmon
SitePoint Addict
 
cholmon's Avatar
 
Join Date: Mar 2004
Location: SC
Posts: 211
Also, re: sessions, you can run memcached on a pool of servers (or the same machine as your web server), and configure PHP to use it as your session handler instead of the default file storage. Much more scalable that way. As with most things in PHP, TMTOWTDI.
__________________
Drew C King: PHP Developer
cholmon is offline   Reply With Quote
Old Feb 4, 2010, 11:46   #22
PalaDolphin
SitePoint Member
 
Join Date: Jan 2010
Posts: 23
cholmon,
Aren't you a little young to be playing with servers? (Your photo.)
PalaDolphin is offline   Reply With Quote
Old Feb 4, 2010, 12:21   #23
pmw57
Unobtrusively zen
bronze trophy
 
pmw57's Avatar
 
Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
Quote:
Originally Posted by PalaDolphin View Post
Yes, but isn't OOP code w/o comments academic masturbation? Joomla code is my introduction to OOP PHP and it has nothing in the way of comments. It's open source, so check for yourself: Joomla version 1.5.15. It's like constructing a building with no blueprints.
There is a trend to remove useless comments from code, or to improve code so that it doesn't require comments..

Chapter 4 of Clean Code covers the topic in quite full detail.

Basically, comments don't make up for bad code. You should explain yourself in the code rather than relying on comments to do the explaining for you. Comments that explain the basic intent of the code can be okay, but it's better ti use the name of the functions to do that. Explanations of intent appear to be the best use for comments.

Often times comments are redundant, where they provide no more information than can be gleaned from reading the code itself, and often-times comments can be misleading. It's all-too easy for code to be updated but to leave the comments are they were. This leads to dangerous territory where the reader doesn't know which one should take precedence.

Well named functions and variables go a long way to prevent the need for comment as well.

Here is an example of what they're talking about. These two code samples demonstrate converting a prime generator that is full of seemingly needful comments, to code that requires very little in the way of comments at all.

From GeneratePrimes.java to PrimeGenerator.java
__________________
Javascript Reference, Quirksmode
HTML Validation, JSLint

Paul Wilkins
pmw57 is online now   Reply With Quote
Old Feb 4, 2010, 12:21   #24
cholmon
SitePoint Addict
 
cholmon's Avatar
 
Join Date: Mar 2004
Location: SC
Posts: 211
Quote:
Originally Posted by PalaDolphin View Post
cholmon,
Aren't you a little young to be playing with servers? (Your photo.)
i'm not playing, i'm doing big-boy work too!
__________________
Drew C King: PHP Developer
cholmon is offline   Reply With Quote
Old Feb 4, 2010, 12:22   #25
Czaries
if(ie6) { upgrade(firefox3); }
 
Czaries's Avatar
 
Join Date: May 2004
Location: Central USA
Posts: 774
As with any codebase, everything depends on how it's been setup.

In theory, well-designed OOP architecture will be much more efficient than procedural, because it breaks things up into individual components by nature. So in many cases when you want to do something simple with a good OOP setup, you can just include and use the few components you need instead of including large 'functions' files with lots of defined functions in them that do a bunch of different things. OOP design can also yield other efficiencies by doing work for you in the most efficient way without you even realizing it or thinking about it, as many ORMs do.

In my personal and professional experience, very few codebases that I have worked with that are supposed to be "object oriented" actually are, and they do indeed wind up being much heavier and more clumsy to work with, especially as the codebase grows over time. I have been working on a project recently that I actually really do wish was just php4-style spaghetti php, because the supposed "object oriented" design really is that bad. That's painful and tough for me to say, because I myself am a huge proponent of using object oriented methodologies - It's just that I very rarely see them used and applied in ways that I feel they should be in the real world, which seems to be what you are seeing as well.
__________________
InvoiceMore - Online Billing & Invoicing
phpDataMapper - Object-Oriented PHP5 Data Mapper ORM
Twitter | Blog | Online Javascript Compressor
Czaries is offline   Reply With Quote
Reply

Bookmarks

Tags
oop

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 01:08.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved