Forum Moderators: coopster

Message Too Old, No Replies

Performance Implications Of Object Orientation

         

iJeep

11:54 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



I am working on building a new site. I am thinking about doing it all in PHP using OO techniques.

I have heard people mention in passing that web pages using OO doesn't make sense because there will usually only be one instance of any given object per page load.

I know there might be other security benefits, but does anybody have information on performance of a php OO page compared to a sequential php page?

coopster

2:57 am on Apr 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Performance may not be the real issue here. It may be the ease of code maintenance or the ability to use code over and over again. Classes can be very handy for these purposes. Also, I would say that a properly written class versus properly-written sequential code is a fine line. Performance should not be the issue.

As far as the discussion you mentioned ...


web pages using OO doesn't make sense because there will usually only be one instance of any given object per page load

... OK, but what happens when you expand beyond the initial instance, now what? Try to write more sequential processing code? Try to push it into functions within the same script? One of the best reasons for classes is the multiple instances. What if you are selling cars online and your end user has specified an interest in four different models? If your class has been structured to accommodates these models, you can load four different instances of the *cars* object in one script for further processing.

I'm not trying to sell classes and objects to you here and neither am I trying to say you cannot get along without them. But an understanding of when they fit into your design is a good thing. Play around a bit and you will be able to determine for yourself if they fit the design. Each application is different, there is no "one-size-fits-all" answer.

Halfdeck

2:12 pm on Apr 1, 2006 (gmt 0)

10+ Year Member



I can't say classes slow my pages down, though I try to use cached elements as much as possible to minimize load on server CPU.

As for only using one instance of a class per page load, I have thought that myself when I first started with PHP classes, but usually I work with multiple classes per page. Doing the same using function calls would be a nightmare.

If you decide on using PHP classes, I'd highly recommend PHP 5+, which allows you to call up any class objects at will using autoloader.