tags:

views:

88

answers:

5

Hello,

I Want to check whether a function is called for the last time. Consider the following sample code,

function foo(){
    if( this is the last call ) {
         echo 'this is the last call of this function';
    }
}

foo(); // should not print
foo(); // should not print
foo(); // since this is the last call, it should print

In my project, i need the condition statement to be present within the function.

I had a idea of using constants/global variables/counters but don't no how to implement.Any ideas to detect the last call of the function ?

+1  A: 

Have you tried using the built-in shutdown function?

Robin
A: 

I don't really think that's possible. You have no way of knowing when the last call will be.

Edit: Globals won't help. As someone else said, you're trying to predict the future. Imagine you've decided to turn off your mobile phone at the conclusion of the last call you've received for the day. You have no control over who might choose to call you or when.

Dave
any ides in using global varibles ?
Aakash Chakravarthy
@Aakash any ideas why do you need such unusual thing at all?
Col. Shrapnel
A: 

You try to predict the future - which is not possible. But, you can emulate this. The function will do it's stuff every time, you will cache the result of each time.
You will use the result from the last time only.
On the other hand, there is a possibility in php.ini to auto append a script to the end of the process. You can put the function call over there. (Or use the register shutdown mentioned above). One last thing, It seems you have a serious flow in your design, or can you elaborate?

Itay Moav
A: 

If you are new to PHP, and if I assume correctly the intent of this program, it is not best practice to hold such a logic: the function guesses and decides by itself when it is last called. This behavior should be driven at a more global scope.

For instance you could use a boolean

  function foo($last=false){
      if( $last ) {
           echo 'this is the last call of this function';
      }
  }

  foo(); // should not print
  foo(); // should not print
  foo(true); // since this is the last call, it should print
ring0
+1  A: 

If you know where in the code the last call occurs, you could do this with a global variable,e.g.

function foo(){
    if($GLOBALS['debug_foo']) {
         echo 'this is the last call of this function';
    }
}

$GLOBALS['debug_foo']=false;

foo(); // should not print
foo(); // should not print

$GLOBALS['debug_foo']=true;

foo(); // since this is the last call, it should print

See the PHP manual page on variable scope for more help.

If you can't tell in the code when the last call is, you can use register_shutdown_function, e.g.

function shutdown()
{
    echo $GLOBALS['foo_dump'];
}

function foo()
{
    $GLOBALS['foo_dump']='record some information here';   
}

//make sure we get notified when our script ends...
register_shutdown_function('shutdown');

foo(); // should not print
foo(); // should not print
foo(); // won't print anything, but when the script ends, our
       // shutdown function will print the last captured bit
       // of diagnostic info
Paul Dixon
Hey Paul, got any idea what such a thing can be used for?
Col. Shrapnel
Well, as you commented, we can't tell exactly what he is trying to solve, but it's clear he's not sure how PHP globals work. So, this might help in that regard!
Paul Dixon