PHP Classes
elePHPant
Icontem

miXin: Extend classes by using several mixin classes

Recommend this page to a friend!
  Info   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2011-08-12 (5 years ago) RSS 2.0 feedNot enough user ratingsTotal: 346 All time: 6,644 This week: 935Up
Version License PHP version Categories
php-mixin 0.1GNU Lesser Genera...5.0PHP 5, Language, Code Generation
Description Author

This class can extend classes by using several mixin classes.

It can load the code of two or more class files and create a new class definition in such way that the first is extended by the others.

The loaded classes are cached until the class files are modified to avoid the overhead of restacking the class files every time they are loaded.

Innovation Award
PHP Programming Innovation award nominee
September 2011
Number 9
Usually if you want to extend a class you need to define one class explicitly as an extension of another.

This class provides an alternative approach that allows that a base class be extended by multiple other classes without having to define them as extensions of each other.

Manuel Lemos
Picture of David Boskovic
Name: David Boskovic <contact>
Classes: 1 package by
Country: Canada Canada
Innovation award
Innovation award
Nominee: 1x

Details
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.	

  Files folder image Files  
File Role Description
Files folder imagestacks (4 files)
Files folder imagetests (4 files)
Plain text file mixin.php Class Class source
Accessible without login Plain text file readme.markdown Doc. Documentation
Accessible without login Plain text file setup.markdown Doc. Documentation
Accessible without login Plain text file stackindex.cache Output Sample output
Accessible without login Plain text file test.php Example Unit test script

  Files folder image Files  /  stacks  
File Role Description
  Accessible without login Plain text file 31c226b21bb94f8daf0715bf913f77ce.php Output Sample output
  Accessible without login Plain text file a87fe4d445124a6a14240eba6f7e185b.php Output Sample output
  Accessible without login Plain text file cca4a4b222dc407169a3397fa0097c11.php Output Sample output
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file base.php Example Unit test script
  Accessible without login Plain text file extend.php Example Unit test script
  Accessible without login Plain text file extend2.php Example Unit test script
  Accessible without login Plain text file extend3.php Example Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:346
This week:0
All time:6,644
This week:935Up