Hi everybody,

recently I stumbled over the register_shutdown_function(string func) function. It tells PHP to execute the function given in string func after processing the entire script. The manual also suggests to use this function as destructor in classes.

Now let's asume I have a class like this:
class foo {
function foo()
echo "this is the constructor";
register_shutdown_function("endthis");
// do stuff...
}

//--- more functions

function endthis() {
// this is the destructor
exec("touch /tmp/" . date("YmdHis"));
// touching a file to verify execution
}
}

The function endthis() is NOT executed. I tried several other calls like:
register_shutdown_function("this->endthis"); with $, without $, escaped, ... but to no effect.

It seems to me that only non_class functions can be called with this function, which would prove the manual wrong.
Has anybody experienced the same or knows what I did wrong? Also, are there other good methods to implement destructors?

Thanks in advance,

Dominique

P.S.: Plattform: Linux, Apache 1.3, PHP 4.0.6

    • [deleted]

    It doesn't work because you are trying to register a method of an object instead of a function.

    Register_shutdown_function registers functions, not methods of objects that may not even exist by the time the shutdown is called.

    "The manual also suggests to use this function as destructor in classes."

    it suggest you use it to simulate destructors.

    "Also, are there other good methods to implement destructors?"

    One method that pops to mind: register a shutdown function, have that function go through the $GLOBALS array and check each var to see if it's an object. If it is, call it's destructor method.


    A forum, a FAQ, email notification, what else do you need?

    Time to YAPF!: http://www.hvt-automation.nl/yapf/

      Hi vincent

      "The manual also suggests to use this
      function as destructor in classes."

      it suggest you use it to simulate
      destructors.

      Hm yeah you're right. I wonder what use does the simulation of destructors from outside of the class have though. Nevermind, I guess this is one of the strange things the PHP folx keep coming up with.

      One method that pops to mind: register a
      shutdown function, have that function go
      through the $GLOBALS array and check each
      var to see if it's an object. If it is,
      call it's destructor method.

      Hmm, this looks a little like the method used by PEAR. The destructor functions would have to have identical or at least "guessable" names to be found by the meta-destructor function but that should be no big deal. The only problem I have with this is that the script has to call destructors from outside rather than from inside the classes. But maybe that's just me...

      Thanks,

      Dominique

        Write a Reply...