PHP Classes

File: readme.markdown

Recommend this page to a friend!
  Classes of David Boskovic   miXin   readme.markdown   Download  
File: readme.markdown
Role: Documentation
Content type: text/plain
Description: Documentation
Class: miXin
Extend classes by using several mixin classes
Author: By
Last change:
Date: 12 years ago
Size: 1,924 bytes
 

Contents

Class file image Download
The Issue === Let's say you have a core class in your system that you reference in many places. But you want to allow people to write extensions to it. You want to be able to add multiple extensions to the same class and you want each one to use the same syntax. ### Your Base Class // extend me! class BaseClass { public $iambase = 1; } ### Extension #1 // John writes a cool new feature class JohnsCoolFeature extends BaseClass { public $iamjohn = 1; public $extended = 1; } ### Extension #1 // Jane writes a cool new feature class JanesCoolFeature extends BaseClass { public $iamjane = 1; public $extended = 2; } In this case, John and Jane couldn't both write extensions to the original object and have them implemented. They would have to manually extend each other and that makes it very difficult to maintain and manage. Moreover when you implement the code in your system. This will be the result $foo = new BaseClass; echo $foo->extended; // null echo $foo->iambase; // 1 echo $foo->iamjohn; // null echo $foo->iamjane; // null The Solution === This mixin utility takes all the different objects and restacks them in a special cache file. Once it has been restacked. You can easily include this file and bingo, it's ready to rock. Now this works! $foo = new BaseClass; echo $foo->extended; // 2 echo $foo->iambase; // 1 echo $foo->iamjohn; // 1 echo $foo->iamjane; // 1 How To Use === _You must never have more than one class in each file._ You can't properly include the original file as it will hijack the classname we want to adjust. So classes you want to make sure are stacked, you include like this. e_MixinUtil::load($file); All you need to do is make sure the extending classes are built like: myClass extends BaseClass { } The base class may be a completely standalone class or it may extend other objects. These other objects will not be touched.