Created
October 15, 2009 06:55
-
-
Save hatyuki/210739 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // vim: ts=4 sts=4 sw=4 | |
class Mixin | |
{ | |
private $mixins = array( ); | |
private $strict = false; | |
function __construct ($include=array( )) | |
{ | |
if ( !is_array($include) ) { | |
trigger_error('Invalid Argument', E_USER_ERROR); | |
} | |
if ( $key = array_search('__strict', $include) ) { | |
$this->strict = true; | |
unset( $include[$key] ); | |
} | |
foreach ($include as $inc) { | |
$inc = is_object($inc) | |
? $inc | |
: new $inc; | |
$this->mixin($inc); | |
} | |
} | |
function __call ($func, $args) | |
{ | |
if ( array_key_exists($func, $this->mixins) ) { | |
$class = array($this->mixins[$func], $func); | |
return call_user_func_array($class, $args); | |
} | |
trigger_error('Call to undefinded function: '.$func, E_USER_ERROR); | |
} | |
private function mixin ($obj) | |
{ | |
$methods = get_class_methods($obj); | |
foreach ($methods as $m) { | |
if ( array_key_exists($m, $this->mixins) && $this->strict ) { | |
trigger_error("Cannot redeclare $m", E_USER_ERROR); | |
} | |
else if ( !preg_match('/^__/', $m) ) { | |
$this->mixins[$m] =& $obj; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment