通过添加下面提到的助手黑客,您可以通过调用将所有内容从hamcrest名称空间导入到当前名称空间:
import_namespace('Hamcrest', __NAMESPACE__);
以下是黑客,函数别名
http://www.php.net/manual/en/function.class-alias.php
除非用于功能:
function function_alias ($original, $alias) {
$args = func_get_args();
assert('count($args) == 2', 'function_alias(): requires exactly two arguments');
assert('is_string($original) && is_string($alias)', 'function_alias(): requires string arguments');
// valid function name - http://php.net/manual/en/functions.user-defined.php
assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][\\\\\\\\a-zA-Z0-9_\x7f-\xff]*$/\', $original) > 0',
"function_alias(): '$original' is not a valid function name");
assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][\\\\\\\\a-zA-Z0-9_\x7f-\xff]*$/\', $alias) > 0',
"function_alias(): '$alias' is not a valid function name");
$aliasNamespace = substr($alias, 0, strrpos($alias, '\\') !== false ? strrpos($alias, '\\') : 0);
$aliasName = substr($alias, strrpos($alias, '\\') !== false ? strrpos($alias, '\\') + 1 : 0);
$serializedOriginal = var_export($original, true);
eval("
namespace $aliasNamespace {
function $aliasName () {
return call_user_func_array($serializedOriginal, func_get_args());
}
}
");
}
与命名空间导入程序结合使用:
function import_namespace ($source, $destination) {
$args = func_get_args();
assert('count($args) == 2', 'import_namespace(): requires exactly two arguments');
assert('is_string($source) && is_string($destination)', 'import_namespace(): requires string arguments');
// valid function name - http://php.net/manual/en/functions.user-defined.php
assert('preg_match(\'/^([a-zA-Z_\x7f-\xff][\\\\\\\\a-zA-Z0-9_\x7f-\xff]*)?$/\', $source) > 0',
"import_namespace(): '$destination' is not a valid namespace name");
assert('preg_match(\'/^([a-zA-Z_\x7f-\xff][\\\\\\\\a-zA-Z0-9_\x7f-\xff]*)?$/\', $destination) > 0',
"import_namespace(): '$source' is not a valid namespace name");
foreach(get_declared_classes() as $class)
if (strpos($class, $source . '\\') === 0)
class_alias($class, $destination . ($destination ? '\\' : '') . substr($class, strlen($source . '\\')));
$functions = get_defined_functions();
foreach(array_merge($functions['internal'], $functions['user']) as $function)
if (strpos($function, $source . '\\') === 0)
function_alias($function, $destination . ($destination ? '\\' : '') . substr($function, strlen($source . '\\')));
}