Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Created January 6, 2013 17:54
Show Gist options
  • Save ngyuki/4468971 to your computer and use it in GitHub Desktop.
Save ngyuki/4468971 to your computer and use it in GitHub Desktop.
[qiita 20130107] クラスの静的メソッドを名前空間にインポートして修飾なしで呼ぶ
<?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