Languages like Ruby have in-built support for Mixins, which is what you want. Something roughly similar could be hacked up using PHP's lambda function support.
Never tried to write this sort of thing before, but here is something that might work
class MixinFaker
{
private
$funcs = array();
/**
* Creates an anonymous (lambda) function on the $func array
*/
public function addFunction( $name, $args, $code )
{
$this->funcs[$name] = create_function( $args, $code );
}
/**
* Looks like your using so PHP5 so heres the __call magic to make the
* lambda functions appear to be part of the class
*/
public function __call( $name, $args )
{
if( array_key_exists( $name, $this->funcs ))
{
return call_user_func_array( $this->funcs[$name], $args );
}
return false;
}
}
$fake = new MixinFaker();
$fake->addFunction( 'add', '$a', 'return ++$a;' );
$fake->addFunction( 'replace', '$a, $b, $c', 'return str_replace( $a, $b, $c );' );
echo '1 + 1 = ' .
$fake->add( 1 ) . '<br>';
echo 'String replacement: ' .
$fake->replace( array( 'cats', 'dogs' ), array( 'hats', 'cogs' ), 'It was raining cats and dogs' );
Which outputs
1 + 1 = 2
String replacement: It was raining hats and cogs