Article
Faking Dual Inheritance Classes in PHP
Object oriented coding in PHP can save a lot of time in your projects, but currently, PHP only allows classes to have a single parent. Unfortunately, many Internet classes are ripe for using dual inheritance -- particularly if you want to have one class serve your visitors, while a child class serves your administrators. Faking dual inheritance is not easy, nor does it have all the advantages of true dual inheritance, but I've already done most of the work, so read on with confidence that this will be a real time saver!
I've split this tutorial into a number of sections, so you can go straight to what you want. All the files mentioned here are available below. If you "only" write scripts in PHP, don't have a firm grasp on classes, or just don't see the point of this article, keep reading. Those who already use classes, but don't know why they would want a dual inheritance class, should probably just skip to "But Why Dual Inheritance?" If on the other hand, you have already found yourself wishing for dual inheritance classes in PHP, just head straight for "How to Fake It."
The Files
The archive below contains universalcontent.php, universalcontentadmin.php, pagecontent.php, pagecontentadminpre.php , pagecontentadmin.php, universalupdater.php, their documentation files, and a few "demo" files to help users figure out the details. You'll need all these files to complete the tutorial.
Download the Code Archive and get started!
Why OO?
Object oriented programming is a wonderful method to reduce time on projects, and keep your work much more organized. If there's any task that you need to repeat between projects, if you've ever scavenged code from one of your old projects to use on a new one, or if you have ever needed to write more than just "a little" code, then you really should consider using object oriented programming to simplify your work.
Classes are the main component of object oriented programming in PHP (and more than a few other languages). While they may initially seem excessive for your scripts -- even as one with a background in C++, I wasted a lot of time by not using classes when I started with PHP -- classes will save you a lot of time in the long run.
So, what really is a class? Classes are a collection of variables and the functions that interact with them. They are very easy to define:
class myclass
{
var myclassvariable;
var . . . etc.
function myfunction ();
function . . . etc.
}
Now that you have a class, you don't need to keep track of its inner workings. You simply create a new instance each time you want to use it:
myobject = myclass (some_arguments);
myobject->myfunction ();
One of the great things about classes is these little "packages" of code are so easy to recycle both within a project, and between projects. A little foresight combined with PHP's extremely flexible variables can save you a tremendous amount of time. In my own programming I use a class called universalcontent. It handles all the basic calls to any MySQL table I might need in a project. While the class is simple, it forms a building block for many of my more complex classes.
Time for Children
While classes themselves are useful for keeping your code clean, and helping you recycle some of it, child classes really make object oriented programming what it is. Consider the previous universalcontent class -- by itself I can recycle some code, but if that's all I accomplish, I'm not really helping myself that much by having a class that's only capable of simple queries to a site's database.
Child classes extend a parent so that you can create a new, more specific class. This way, you create "common denominator" parent classes, and then either add new functions and variables, or overload, that is: create a new version of, old functions. The child class automatically has all the functions and variables of the parent. This is called single inheritance.
My universalcontent class now has its child universalcontentadmin -- a powerful class combination. Now, not only can I easily access any basic database table for my visitors, but I can also create an administrative interface for that table with almost no coding.

This kind of parent-child relationship allows me to reuse code, and at the same time makes debugging and future changes much easier. Why are future changes easier? Say that I manage all my database connections through universalcontent functions, and that universalcontentadmin exclusively uses those functions when it accesses the database. If, later, I change from MySQL to some other SQL database, I merely have to make changes to universalcontent. After that, it and all of its children will automatically be upgraded to support the new database.
Art is a freelance Web designer and developer based in Bloomington, Indiana, and the sole operator of