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.

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.

The Drupal code you showed above is indeed crap.

The code I inserted was from Joomla, not Drupal.

Thanks for the APC info.

Oops! My bad.

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.

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.

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?

Not as we understand it, but Facebook have just recently created their own.
http://developers.facebook.com/news.php?blog=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.

pmw57,
This HipHop for PHP was released this morning? Converting PHP into C++ sounds intriguing. I’d like to play with it. YACC!

Yep, this morning - my news wire has been running hot.

Using OOP in PHP is, generally speaking, not a performance hazard. Joomla is slow, not because it uses objects, but because it’s crap.

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.

OOP PHP changed my life. Profile your code, Try some Singleton and Registry Patterns too.

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.

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.

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

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.

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.

Good point. A nice example of when to not use them is this Hello world! using design patterns

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.