所以我正在尝试创建一个变量类,这意味着我可以动态命名我的变量。我想在我的代码上有一个undeclared variable at line x
。我设法让一个函数被这样调用:
function ($varname, $line = 0) {
// Example
echo 'unknwon variable found at line '.$line;
}
但我希望能够删除函数的line = 0
,而只给它一行代码,而不需要调用以下函数:
exampleFunction('Name', __LINE__);
相反,称之为:
exampleFunction('Name');
并且__LINE__
变量可以随它一起传递,而不需要包含它。-我试着让被调用的函数看起来像这样:
exampleFunction($varname, __LINE__) {
// Executed code.
}
尽管这也没有奏效,但还是犯了一个错误。
您可以在exampleFunction
中使用debug_backtrace
方法来获取带有行的调用者信息。
http://php.net/manual/tr/function.debug-backtrace.php
示例(从php.net复制)
<?php
// /tmp/a.php dosyası
function a_test($str)
{
echo "'nHi: $str";
var_dump(debug_backtrace());
}
a_test('friend');
?>
<?php
// /tmp/b.php dosyası
include_once '/tmp/a.php';
?>
哪个输出:
Hi: friend
array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}