开发者

Tell How Function was 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?


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:

echo function1();

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.

function hello(){
  return "Hello!";
}
echo hello(); // prints Hello!


function hello(){
  echo "Hello!";
}
hello(); // prints Hello!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜