Skip to content


Subversion checkout URL

You can clone with
Download ZIP
PHP Mixin Hack
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


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.


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.

Something went wrong with that request. Please try again.