Article

Faking Dual Inheritance Classes in PHP

Page: 1 2 3 4 Next

But Why Dual Inheritance?

So, why would anyone want dual inheritance? The simplest answer is: because you've found a situation where you want the abilities of two classes to form the basis of one new class. In the previous sections, I talked about my parent universalcontent and its child universalcontentadmin, which form the basis of my database interactions for visitors and administrators. I also have a class, pagecontent, which I use to access page content that has been stored in a database, and also to drive dynamic menus. Of course, there is another class, called pagecontentadmin. Where should it go in the family tree?

903_dualinheritance

Ideally, it would be a child of both pagecontent and also universalcontentadmin, as it will surely need access to the pre-existing administration code and, to a lesser extent, it needs access to some of the pagecontent functions, and constants.

So, what options do we have? Unfortunately, there aren't many. We can copy-and-paste either the universalcontentadmin or pagecontent functions into our new child class, but we were trying to avoid just that very situation when we decided to use object oriented programming. Another option is to simply change the structure of our classes -- to make universalcontent and universalcontentadmin one class. This is not a particularly attractive option, as adding universalcontentadmin to universalcontent would more than quadruple the parent class's size (thereby slowing the server down) and offer no benefits for visitors to the site. Single inheritance is really not ideal here.

How to Fake It

So, how can I make a pagecontentadmin class, which effectively has two parents (pagecontent and universalcontentadmin)? Unfortunately, PHP does not allow for multiple inheritance, nor does it allow for strategically placing "include" statements inside a class bracket -- you can use them as parts of functions, but not as part of the class. Either one of those would give us a solution with either two parents, or a pretty close approximation of one.

Instead, I am forced to create a "pre-class" to get all the functions from universalcontentadmin into the new class pagecontentadmin.

903_prepagecontent

This would be a very bleak place to end our story, as we have enjoyed almost none of the advantages of object oriented programming. If later I improve universalcontentadmin, I will have to manually make changes to the prepagecontentadmin class -- a process which promises not only to be tedious, but potentially will introduce bugs as I haphazardly copy and paste to every pre-code I can find.

Happily, though, it does not end there. By creating a new class, universalupdater, we can eliminate most of the disadvantages of this method. This class will search through a directory and all its subdirectories looking for any PHP file with the proper comment tags. Then, it will take the source class (in my examples, universalcontentadmin), modify it, and insert the class into the target files. Universalupdater is designed to work with a single source class per file and a single "target class" per file. While you can have other classes in the same file without a problem, I have designed the script to work only with one "active" class per file.

The steps to use universalupdater are quite simple:

  1. Create the parent functions and organize your files

  2. Prepare the source file

  3. Prepare the intermediate file

  4. Create an instance of universalupdater and call the function "Execute". This step shoule repeated any time you update the source class

  5. Use the newly created intermediate class as a parent of your "dual inheritance" child class

General Preparations

Hopefully, it goes without saying that before you can have a dual inheritance class, you need to have two parent classes already prepared. What may need to be mentioned is that in order for the dual inheritance class to be effective, you will need to make sure the classes are compatible. In my example, pagecontent and universalcontent are sibling classes (i.e. the share the same parent universalcontent). Not only do these two not have any conflicting functions (or functions with the same name which might accidentally get overloaded), but they also share the same basic data structure.

Once you have the files (both your own files and universalupdater), you will need to make sure you have them organized in a way that will allow universalupdater to function efficiently. On my own system, all my projects, and the classes that are shared between various projects, are in subdirectories of "c:\htdoc" (the directory that Apache serves). This makes it really easy for the universalupdater to do its work: I just tell it to update anything in its parent directory and subdirectories. Automatically, it will scan all my projects and all my general classes for the files that need to be edited.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links