Article

Faking Dual Inheritance Classes in PHP

Page: 1 2 3 4 Next

Prepare the Source File

Prepping the source file takes very little time at all (merely insert comment tags to indicate where universalupdater should start and end its copying). The relevant parts of the universalcontentadmin class are shown below. The two tags needed for the universalupdater are in blue -- the first appears right before I defined the constants that will be used in the universalcontentadmin, and the second occurs right after I finish defining the class. I have added two descriptions in green. Hopefully, it's self-evident that they don't actually appear in the class itself.

<?  
//Class UniversalContentAdmin  
 
include_once ((isset($phplocation)? $phplocation.'universalcontent.  
php' : 'universalcontent.php'));  
 
//Replacement Code Starts Here  
 
if (!defined ('UNIVERSALADMIN_CONSTANTS'))  
{  
 define ('UNIVERSALADMIN_CONSTANTS', 'defined');  
 define ('UCA_NO_VALUE', '@#784pdjas');  
 define ('UCA_BLANK_VALUE', '*^@adfds');  
 define ('UCA_EDIT', 'ucedit');  
 define ('UCA_DUPLICATE', 'ucdup');  
 define ('UCA_DELETE', 'ucdelete');  
 define ('UCA_CONFIRM', 'ucconfirm');  
 define ('UCA_UPDATE_ITEM', 'ucupdate');  
 define ('UCA_DISPLAY_NAME_SPLITTER', '||');  
}  
 
class universalcontentadmin extends universalcontent  
{  
 var $tableformat;  
 var $comments;  
 var $itemcount;  
 var $defaultpadding;  
   
 function universalcontentadmin ($paramtablename)  
 .  
 .  
 .  
 <THE REST OF THE CLASS'S FUNCTIONS>  
 .  
 .  
 .  
 function ProcessMinorSpecialItem ($variablename, $value, &$warning,    
 &$reporttext)  
 {  
   <LAST FUNCTION IN THE CLASS>  
 }  
}  
 
//Replacement Code Ends Here  
?>

Prepare the Intermediate File

The code that creates the new intermediate class is almost as simple. You can include this intermediate class either in the same file as your final class, or as a separate file. For this example, I've made the pre-class a separate file. It's easier to demonstrate that way. It's also easier to edit the final class when there is not a bunch of stuff above it, not to mention that my editor starts to slow down slightly if it has to edit 1000+ line PHP files. As this is code for an administrator, and as such, will not be used often, I don't mind if the server needs to fetch one additional file.

Here's how my intermediate file looked before I ran the updater.

<?  
//PrePageContentAdmin Class  
//This class extends PageContent with the code from UniversalAdmin.    
//It is maintained automatically by universalupdater.  
 
include_once ((isset($phplocation)? $phplocation.'pagecontent.php' :    
'pagecontent.php'));  
 
//parent class= pagecontent  
//child class= prepagecontentadmin  
//source class= universalcontentadmin  
//Replacement Code Starts Here  
//Replacement Code Ends Here  
?>

As you can see, it's simply a single include_once statement (loading its parent, pagecontent) and a number of comments. So what are all the comments? The five last comments are all you need to make the universalupdater class work. Code is copied from the source class (in this case universalcontentadmin) and pasted between the two replacement code tags. When that's done, universalupdater will make two changes to the class. First, the source file's class definition will be changed to "class my_child extends my_parent" ("my_child" and "my_parent" are taken from the respective "//child class" and "//source class" tags).

The second change is that, if the source file has a constructor function, it will be duplicated and renamed to match the child class. The "//source class" tag lets universalupdater know which source file should be used with a given file. If it doesn't match the file with which universalupdater is working, then no replacements will be made. The equal signs in the parent, child, and source classes can optionally have spaces before or after them, so "//child class = prepagecontentadmin", "//child class =prepagecontentadmin", "//child class= prepagecontentadmin", and "//child class=prepagecontentadmin" are all equally valid.

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

Sponsored Links