可能重复:
PHP 5 中的调用函数?
我想知道从哪里调用全局函数或公共方法。我想我可以通过检查 debug_backtrace 来做到这一点,但我宁愿使用一种更轻量级的机制(如果存在)。有什么建议吗?
例如,如果您想象 get_callee() 函数和常量存在:
function doSomething() {
if(get_callee() == 'PHP_GLOBAL') { throw new IllegalAccessException(); }
...
}
编辑:抱歉,现在看到你关于 debug_backtrace() 的注释。
有点难看,但是嘿,如果你需要这样做,那就错了。
神奇之处在于 get_callee() 函数和 debug_backtrace()。是的,如果您必须使用它,请添加一些错误检查。
<?php
init();
function foo()
{
echo 'bar called from ' . get_callee() . '<br />';
bar();
}
function bar()
{
echo 'foo called from ' . get_callee() . '<br />';
}
function init()
{
echo 'init.. <br />';
foo();
}
function get_callee()
{
$backtrace = debug_backtrace();
return $backtrace[1]['function'];
}
输出:
初始化..
bar 从 foo 调用
foo 从酒吧打来电话
Xdebug提供了一些不错的功能。
<?php
Class MyClass
{
function __construct(){
$this->callee();
}
function callee() {
echo sprintf("callee() called @ %s: %s from %s::%s",
xdebug_call_file(),
xdebug_call_line(),
xdebug_call_class(),
xdebug_call_function()
);
}
}
$rollDebug = new MyClass();
?>
将返回踪迹
callee() called @ /var/www/xd.php: 16 from MyClass::__construct
要在 ubuntu 上安装 Xdebug 最好的方法是
sudo aptitude install php5-xdebug
您可能需要先安装 php5-dev
sudo aptitude install php5-dev
为什么不简单地使用面向对象并声明你的方法/函数私有?
如果你开始在你的代码中撒满这些
get_callee()
,你正在创造一个可怕的混乱。