|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Member
Join Date: Jan 2010
Posts: 23
|
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 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() 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. |
|
|
|
|
|
#3 | |
|
SitePoint Member
Join Date: Jan 2010
Posts: 23
|
Quote:
Thanks for the APC info. |
|
|
|
|
|
|
#4 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
Oops! My bad.
__________________
Me on StackOverflow | Blog & personal website. I mostly use: PHP, Java, JavaScript, Android. |
|
|
|
|
|
#5 |
|
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. |
|
|
|
|
|
#6 |
|
Unobtrusively zen
![]() ![]() 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. |
|
|
|
|
|
#7 |
|
SitePoint Member
Join Date: Jan 2010
Posts: 23
|
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 |
|
Unobtrusively zen
![]() ![]() 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. |
|
|
|
|
|
#9 |
|
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! |
|
|
|
|
|
#10 |
|
Unobtrusively zen
![]() ![]() Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
|
Yep, this morning - my news wire has been running hot.
|
|
|
|
|
|
#11 | ||
|
Community Advisor
![]() ![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 6,067
|
Quote:
Quote:
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.
__________________
Introducing pearhub | Konstrukt 2.3.1 | twitter.com/troelskn |
||
|
|
|
|
|
#12 |
|
John 8:24
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Florida
Posts: 1,304
|
OOP PHP changed my life. Profile your code, Try some Singleton and Registry Patterns too.
|
|
|
|
|
|
#13 |
|
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. |
|
|
|
|
|
#14 |
|
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.
|
|
|
|
|
|
#15 |
|
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. |
|
|
|
|
|
#16 |
|
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.
|
|
|
|
|
|
#17 |
|
Unobtrusively zen
![]() ![]() 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. |
|
|
|
|
|
#18 | |
|
Life is short. Be happy today!
![]() ![]() ![]() Join Date: Apr 2003
Location: Los Angeles, Bangkok, Manhattan, Louisiana
Posts: 3,747
|
Quote:
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 |
|
|
|
|
|
|
#19 | |
|
Unobtrusively zen
![]() ![]() Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
|
Quote:
|
|
|
|
|
|
|
#20 | |
|
SitePoint Member
Join Date: Jan 2010
Posts: 23
|
Quote:
|
|
|
|
|
|
|
#21 |
|
SitePoint Addict
![]() ![]() ![]() 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 |
|
|
|
|
|
#22 |
|
SitePoint Member
Join Date: Jan 2010
Posts: 23
|
cholmon,
Aren't you a little young to be playing with servers? (Your photo.) |
|
|
|
|
|
#23 | |
|
Unobtrusively zen
![]() ![]() Join Date: Jan 2007
Location: Christchurch, New Zealand
Posts: 8,039
|
Quote:
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 |
|
|
|
|
|
|
#24 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Mar 2004
Location: SC
Posts: 211
|
Quote:
![]()
__________________
Drew C King: PHP Developer |
|
|
|
|
|
|
#25 |
|
if(ie6) { upgrade(firefox3); }
![]() ![]() ![]() ![]() ![]() 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 & InvoicingphpDataMapper - Object-Oriented PHP5 Data Mapper ORM Twitter | Blog | Online Javascript Compressor |
|
|
|
![]() |
| Bookmarks |
| Tags |
| oop |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 01:08.





. Use objects like crazy.










Linear Mode
