确定调用该函数的名称空间 [英] Determine what namespace the function was called in
问题描述
我想知道是否可以确定调用函数时当前的名称空间是什么.我有此函数声明:
I was wondering if it was possible to determine what the current namespace was when the function was being called. I have this function declaration:
<?php
namespace Site\Action;
function add ($hook, $function) {
/**
* determine the namespace this was called from because
* __NAMESPACE__ is "site\action" :(
*/
if (is_callable($function))
call_user_func($function);
}
?>
在另一个文件上:
<?php
namespace Foo;
function bar () {
}
?>
让我们说这是我的程序代码:
And let's say I have this as my procedural code:
<?php
namespace Foo;
Site\Action\add('hookname', 'bar');
?>
假设在这种情况下Bar
旨在解析为Foo\bar
是有道理的,因为这是从其调用的名称空间.
It would make sense to assume that Bar
in this case was intended to resolve as Foo\bar
since that was the namespace it was called from.
这是一个很长的解释,因此,是否可以确定调用Site\Action\add()
的活动名称空间?
That was a long explanation so again, is it possible to determine the active namespace where Site\Action\add()
was called from?
谢谢.
推荐答案
您正在寻找的是: debug_backtrace() a>是你的朋友.
What you are looking for is : ReflectionFunctionAbstract::getNamespaceName
If you want to know where you're coming from debug_backtrace() is your friend.
以下应解决您的难题:
function backtrace_namespace()
{
$trace = array();
$functions = array_map(
function ($v) {
return $v['function'];
},
debug_backtrace()
);
foreach ($functions as $func) {
$f = new ReflectionFunction($func);
$trace[] = array(
'function' => $func,
'namespace' => $f->getNamespaceName()
);
}
return $trace;
}
只需从任何地方调用它即可查看回溯.
我修改了您的过程"代码文件,如下所示:
Just call it from anywhere to see the backtrace.
I modified your "procedural" code file as follows:
namespace Foo;
function bar ()
{
var_export(backtrace_namespace());
}
/** The unasked question: We need to use the fully qualified name currently. */
function go()
{
\Site\Action\add('hookname', 'Foo\\bar');
}
go();
包含此文件的结果将在stdout上显示如下:
The Result from including this file will be the following on stdout:
array (
0 =>
array (
'function' => 'backtrace_namespace',
'namespace' => '',
),
1 =>
array (
'function' => 'Foo\\bar',
'namespace' => 'Foo',
),
2 =>
array (
'function' => 'call_user_func',
'namespace' => '',
),
3 =>
array (
'function' => 'Site\\Action\\add',
'namespace' => 'Site\\Action',
),
4 =>
array (
'function' => 'Foo\\go',
'namespace' => 'Foo',
),
)
现在,奖励积分为隐藏的问题提供了答案:
Now for bonus points the answer to the hidden question:
以下内容将使您可以按预期的方式调用该函数:
The following will allow you to call the function as you intended:
Site\Action\add('hookname', 'bar');
没有恐惧:
警告:call_user_func()期望参数1为有效的回调,找不到函数"bar"或无效的函数名称
Warning: call_user_func() expects parameter 1 to be a valid callback, function 'bar' not found or invalid function name
因此,在重新设计尺寸之前,请尝试以下尺寸:
So before you redesign try this on for size:
namespace Site\Action;
function add($hook, $function)
{
$trace = backtrace_namespace();
$prev = (object) end($trace);
$function = "$prev->namespace\\$function";
if (is_callable($function))
call_user_func($function);
}
我看不出为什么不应该使用debug_backtrace
的原因,这就是它的用途.
I see no reason why debug_backtrace
should not be used, this is what it is there for.
nJoy!
这篇关于确定调用该函数的名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
function backtrace_namespace()
{
$trace = array();
$functions = array_map(
function ($v) {
return $v['function'];
},
debug_backtrace()
);
foreach ($functions as $func) {
$f = new ReflectionFunction($func);
$trace[] = array(
'function' => $func,
'namespace' => $f->getNamespaceName()
);
}
return $trace;
}
namespace Foo;
function bar ()
{
var_export(backtrace_namespace());
}
/** The unasked question: We need to use the fully qualified name currently. */
function go()
{
\Site\Action\add('hookname', 'Foo\\bar');
}
go();
array (
0 =>
array (
'function' => 'backtrace_namespace',
'namespace' => '',
),
1 =>
array (
'function' => 'Foo\\bar',
'namespace' => 'Foo',
),
2 =>
array (
'function' => 'call_user_func',
'namespace' => '',
),
3 =>
array (
'function' => 'Site\\Action\\add',
'namespace' => 'Site\\Action',
),
4 =>
array (
'function' => 'Foo\\go',
'namespace' => 'Foo',
),
)
Site\Action\add('hookname', 'bar');
警告:call_user_func()期望参数1为有效的回调,找不到函数"bar"或无效的函数名称
Warning: call_user_func() expects parameter 1 to be a valid callback, function 'bar' not found or invalid function name
namespace Site\Action;
function add($hook, $function)
{
$trace = backtrace_namespace();
$prev = (object) end($trace);
$function = "$prev->namespace\\$function";
if (is_callable($function))
call_user_func($function);
}
debug_backtrace
的原因,这就是它的用途.debug_backtrace
should not be used, this is what it is there for.