我想看看函数在哪里被调用。
该函数是在 echo 还是 sprintf 中调用的?然后返回,否则回显内容。
我得到了这段代码(test.php):
<?php
function __($str = '')
{
// who is my parent?
$parent=debug_backtrace();
if ( isset ( $parent[1] ) )
$parent = $parent[1]['function']; //Did it!
else
$parent = debug_backtrace()[1]['function']; //Try
if ( empty ( $parent ) )
$parent = "ERROR!";
return sprintf("[%s] %s.%s", $parent, $str, PHP_EOL);
}
__('does nothing');
echo __('test from echo #1');
echo(__('test from echo #2'));
echo sprintf(__('test from sprintf #1'));
echo(sprintf(__('test from sprintf #2')));
?>
当我在终端输入它时,我得到的是:
WDGMBP:Test Wes$ php test.php
[ERROR!] test from echo #1.
[ERROR!] test from echo #2.
[ERROR!] test from sprintf #1.
[ERROR!] test from sprintf #2.
(来自网络的ps相同)
我的 PHP 版本是:
WDGMBP:BIHappyV3 Wes$ php -v
PHP 5.5.27 (cli) (built: Aug 22 2015 18:20:44)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies