Created
January 6, 2013 17:54
-
-
Save ngyuki/4468971 to your computer and use it in GitHub Desktop.
[qiita 20130107] クラスの静的メソッドを名前空間にインポートして修飾なしで呼ぶ
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 | |
function import($ns, $klass) | |
{ | |
$ref = new ReflectionClass($klass); | |
$methods = $ref->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC); | |
$code = "namespace $ns;\n"; | |
/* @var $method ReflectionMethod */ | |
foreach ($methods as $method) | |
{ | |
$func = $method->getName(); | |
if (function_exists("$ns\\$func") === false) | |
{ | |
$code .= "function $func(){ return \\call_user_func_array('$klass::$func', \\func_get_args()); }\n"; | |
} | |
} | |
eval($code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment