讲述函数是如何被调用的
我看过 debug_backtrace 但到目前为止它没有做我需要它做的事情。
我需要知道我正在调用的函数是“被调用”还是“被回显”。像这样:
function hello() {
//blah blah
}
echo hello(); //echo-ed
hello(); //'called'
但是如果函数是“调用”而不是“回显”,它会做不同的事情。
我该怎么做呢?
I've looked at debug_backtrace but so far it doesn't do what I need it to do.
I need to know whether the function I'm calling was 'called' or 'echo-ed'. Like this:
function hello() {
//blah blah
}
echo hello(); //echo-ed
hello(); //'called'
But the function would do different things if it was 'called' over 'echo-ed'.
How would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我很确定这是不可能的。这不起作用的原因是“echo”或任何其他运算符、函数或变量赋值使用您调用的函数的返回值。因此,如果您有以下情况:
发生的情况是 function1 被执行,并且返回值被传递给 echo。因此,function1 不可能知道它的返回值将被“回显”,因为当发生这种情况时,function1() 已经被调用并完成执行。
I am pretty sure that this is impossible. The reason this cannot work is that "echo" or any other operator, function or variable assignment uses the return value of the function you've called. So if you've got the following:
What happens is that function1 gets executed, and the return value is passed to echo. Therefor, function1 cannot possibly know that its return value is going to be "echo-ed", because by the time that happens, function1() has already been called and finished executing.
没有有效的方法来处理它
更新:
没有办法处理它:)
There is no efficient way to deal it
Update:
There is no way to deal it :)
举两个例子来帮助你理解。
two examples to help you understand.