Inheritance to improve performance
May 17, 2007 on 4:24 pm | In PHP |For those of you who went through a CS major in college, you probably know inheritance like the back of your hand. You know how to use it, when is a good opportunity to use it, and you know why… or so you’re told.
If you are a CS student, or a fresh grad working in some company as a programmer, you might be working on object-oriented code with tons of inherited classes — because you were told to do so. However when you get to work on your own projects, especially in a language like PHP, you may not think about it. After all, PHP is a web scripting language — for many small scripts, even procedural code is good enough. You may not even need to use an object-oriented design. How would inheritance come into play?
Practical Inheritance
Suppose you’re working on a huge project in PHP, writing some kind of web-based system. You’ll certainly be working with an object-oriented design in PHP.
Now, when I say practical inheritance, it means that you’ll actually gain something practically when using inheritance; and in our case here, what we gain is performance.
Imagine that you have a class called User. This class may have some 20~ member variables, and quite a few member methods.
class User {
... // member variables
// member methods
function Login()...
... // other login-related functions
function SendMessage()...
... // other messaging-related functions
// etc. etc.
}
When you’re closing in to completion in your system, your backend PHP code, the User class, is getting near 100kb in file size. That’s quite a bit of code!
In PHP, scripts are “compiled” (or “interpreted”) as they are executed. The larger the code, the longer it takes. It may not be much when you are just testing it on your development machine, but when it’s online on a high traffic site, it gets noticeable. Now, it is true that the PHP engine and the web server can optimize a lot of this by, say, caching compiled code, etc. However, you can still help performance a lot by coding nicely.
Now think about your frontend scripts — you’re making an instance of the User class for every GET/POST request, and calling the appropriate methods to do your work. Your frontend scripts are nice and clean, easy-to-read and everything. How nice, right? But think about this: you’re loading the entire User class code for every action taken, and that includes all the member methods that one particular action does not need at all. When a user is just logging in, you’re creating a User instance and all its methods for every action a User can do, and then calling the Login method. Your login.php might look like this…
... // code to process form inputs $myUser = new User; ... // init, etc. $myUser->Login(); ... // return results
So let’s look at your 100kb of User class code again. You may have 4~5 methods dealing with login and logout. Another 4~5 methods for sending a message to another user, checking messages, and other things. This is where you can help.
If you make a base class called BaseUser, it only needs to contain the things that every action must require. For example, all the member variables, as well as the method that authenticates the user — checking the cookies info against the database, making sure the user is logged in. Because that kind of thing needs to be done for every action.
Then for every other group of similar actions, you can make a subclass. A new class named LoginUser will extend from BaseUser, and contain all the login, logout methods. Another new class named MessageUser can contain the methods for sending and receiving messages. Now, you have files that look like this:
baseuser.php
<?php class BaseUser { ... // member variables function Authenticate() ... } ?>
loginuser.php
<?php require_once('baseuser.php'); class LoginUser extends BaseUser { ... // login methods } ?>
messageuser.php
<?php require_once('baseuser.php'); class MessageUser extends BaseUser { ... // messaging methods } ?>
Instead of having a file of 100kb which contains the User class code, now you have 10 files of 10kb each, with 1 containing the BaseUser class, and the other 9 containing children classes that do different things.
Now, in your frontend scripts, for the scripts that take care of messaging, you can just make an instance of MessageUser — and that won’t have to load all the methods for login/logout, which is not needed at all for this particular frontend script. For every action done, PHP now only needs to read 20kb of backend code instead of 100kb.
How about that for a practical inheritance usage? There are tons of reasons to use inheritance, like code re-use, generalization to a parent class, polymorphing to subclasses and generalizing code… They are all helpful in a “design sense”, and you might not be able to see their benefits when the programs are executed. So if you needed a better reason to use inheritance, here it is — better performance, what more can you ask for? ![]()
5 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
You must be logged in to post a comment.
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^
Save in del.icio.us
StumbleUpon this
your blog is like a tutorial rather than a web dev blog (expecting update on ur project)
Anyway, yay, it is definitely important to keep oop in mind even in php. and yea, separate design and code is another important issues. you might as well make a function purely responsible for making a connection to the mysql database. smartyphp may be useful for you.
next blog it might be a good idea to post something on security, how to avoid ur form from being messed up by a semi colon and so on.
Comment by emily — May 17, 2007 #
yeah, it’s what I meant to write here. If you wanted to read updates on my project, I can give you another link, I have a discussion group elsewhere going on that discusses all aspects of it.
I do indeed have a database driver class responsible for that purpose in there.
Comment by Bigi — May 17, 2007 #
Indeed that inheritance will improve the performance of your ‘file size’. However, there is always a constant battle between process-time and storage (recall of the interview questions that we all love). I can see that if you use inheritance, you would have to access other external file(s) (increase access/overhead time). Also, you are making the stack more complicated (also increase access/overhead time). Lastly, inheritance makes the code harder to read relatively speaking. Considering all of that, is it worth it? Just a thought.
Comment by Kit — May 17, 2007 #
Kit: I see your concerns, as a software engineer. I can’t say for sure about disk access time, as PHP is fairly high-level, I’m not too sure if the overhead in disk access becomes negligible or not at that point.
As for readability — usually, with well-designed code, it shouldn’t make the code harder to read. Perhaps harder to *learn*, but once you figure out the main structure, it helps you get around the code better!
Comment by Bigi — May 23, 2007 #
[...] agum » Inheritance to improve performance - Start the conversation with the other end agum » Inheritance to improve performance [...]
Pingback by Inheritance in Programming Languages — November 4, 2007 #