我知道debug_backtrace,但是这不是我想要的目的。
我看到你坚持不使用回溯函数,但我仍然相信,当你想记录一个函数被调用时,回溯可以派上用场。
这个想法是,你有一个prefpf块代码存储在一个常量,每当你想调试条件评估这个代码。
如果您有任何疑问,if语句将阻止评估任何内容,以便您的代码的速度不受影响。如果它适合你,你可以修改它以满足你的需求,追踪更多的层次。
为了让我的观点成为一个完整的例子,如果我没有理解正确的,这不是你要找的,我的道歉!
要检查的例子,你有一个文件:test.php的
<?php
define ('__SITE_PATH',realpath(dirname(__FILE__)).'/');
ini_set('log_errors', 1);
ini_set('error_log', __SITE_PATH.'my_error_log.log');
include 'test1.php';
include 'test2.php';
define (__DEBUG_EVAL, '
$dbt = debug_backtrace();
error_log(
"\n".
"Parent function file: " . $dbt[1]["file"] . "\n" .
"Parent function class: " . $dbt[2]["class"] . "\n" .
"Parent fiunction name: " . $dbt[2]["function"] . "\n" .
"Par. fiunc. called from line: " . $dbt[2]["line"] . "\n" .
"Child function file: " . $dbt[0]["file"] . "\n" .
"Child function class: " . $dbt[1]["class"] . "\n" .
"Child fiunction name: " . $dbt[1]["function"] . "\n" .
"Child fiunc. called from line: " . $dbt[1]["line"] . "\n" .
"\n"
);
');
test1::a();
?>
这是test1.php
<?PHP
class test1
{
public static function a()
{
test2::b();
}
}
?>
最后是TEST2。PHP
<?PHP
class test2
{
public static function b()
{
if(defined('__DEBUG_EVAL')) eval(__DEBUG_EVAL);
echo 'Hello!';
}
}
?>
这是结果:
[13-Apr-2012 14:37:18]
Parent function file: C:\PHP-GTK\MyProjects\Electre\test1.php
Parent function class: test1
Parent fiunction name: a
Par. fiunc. called from line: 29
Child function file: C:\PHP-GTK\MyProjects\Electre\test2.php
Child function class: test2
Child fiunction name: b
Child fiunc. called from line: 7
http://php.net/manual/en/function.rename-function.php 重命名原始功能,别的东西,用这个名字创建你自己的函数,然后调用里面的原函数。缺点是你必须从PECL安装APD。 – strkol 2012-04-13 19:24:21