SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 47
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Drupal code you showed above is indeed crap.
    The code I inserted was from Joomla, not Drupal.

    Thanks for the APC info.

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pmw57,
    This HipHop for PHP was released this morning? Converting PHP into C++ sounds intriguing. I'd like to play with it. YACC!

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Yep, this morning - my news wire has been running hot.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  12. #12
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP PHP changed my life. Profile your code, Try some Singleton and Registry Patterns too.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  14. #14
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...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.

  16. #16
    Non-Member
    Join Date
    Jan 2010
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  17. #17
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #18
    Life is short. Be happy today! silver trophybronze trophy Sagewing's Avatar
    Join Date
    Apr 2003
    Location
    Denver, Phang-Nga, Thailand
    Posts
    4,379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    The fewer our wants, the nearer we resemble the gods. ó Socrates

    SAGEWING LLC - QUALITY WEB AND MOBILE APPS. PREMIUM OUTSOURCING SERVICES.
    Twitter | LinkedIn | Facebook | Google+

  19. #19
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  20. #20
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  21. #21
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    <?= $short_tags++ ?>

  22. #22
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cholmon,
    Aren't you a little young to be playing with servers? (Your photo.)

  23. #23
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  24. #24
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    <?= $short_tags++ ?>

  25. #25
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •