SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Run code at end of execution?

    Is there any way in PHP to run some code at the end of every execution? I don't mean just placing the code at the bottom of the script, because that won't work with this. For example, in Perl there's the BEGIN { } and END { } constructs. Every time that Perl program finishes execution, whatever code is between END { } will be ran.

    For example, if exit() is called somewhere in the PHP program, I still want a small block of code to be executed. Is there any way to do this in PHP? I looked through the manual, but couldn't find anything. Or is this not supported by PHP?

    Thanks,
    Matt

  2. #2
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the directive auto_append_file in your php.ini file. However it's not called if the script is ended with the exit() function.

  3. #3
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not place the code you want to run in a function called end() and then wherever you call exit() do it as exit(end()); It's not great but it works.

    I'm not sure if you maybe work with OOP alot but the destructor is (sort of) for doing things like this.

  4. #4
    Codehead. hamidof's Avatar
    Join Date
    Dec 2005
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes register_shutdown_function(some_function());, its the best way to do it, check the manual.

  5. #5
    What a twist! Kings's Avatar
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    954
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe the register_shutdown_function() function is what you're looking for?
    Dennis Pallett - NoCertainty - My Personal Weblog
    The Web Network: ASPit | PHPit | WebDev-Articles
    Blogs: TalkFones | Holidayzer | PHPit Blog

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    What a twist! Kings's Avatar
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    954
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's unanimous: register_shutdown_function is what you need
    Dennis Pallett - NoCertainty - My Personal Weblog
    The Web Network: ASPit | PHPit | WebDev-Articles
    Blogs: TalkFones | Holidayzer | PHPit Blog

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! That's exactly what I was looking for. When working with larger programs, it helps a whole lot with things like debugging. I knew the guys over at PHP must have put something like this in.

    Thanks guys!

    Matt

  9. #9
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    Awesome! I never knew that existed

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn, never mind. Seems as though register_shutdown_function() runs on a difference process, so a debug_backtrace() won't work on it. Any way of getting around that one?

    Right now at least, I just want something so when execution ends, a debug_backtrace() can be parsed and appended to a file. Any advice?

    Thanks,
    Matt

  11. #11
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does register_shutdown_function() not work? One thing you need to understand is any variables you feed it are feed to it at the time of being specified and not at the time of the shutdown.

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    How does register_shutdown_function() not work? One thing you need to understand is any variables you feed it are feed to it at the time of being specified and not at the time of the shutdown.
    Do a debug_backtrace() in the shutdown function. That returned array only contains one element, which is the shutdown function itself. All the other functions that were executed are gone.

  13. #13
    SitePoint Addict evertcollab's Avatar
    Join Date
    Apr 2006
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The shutdown function is the last function to be executed and its no longer in the scope of something else...

    If I'm correct you want to call die() at one point and then check out where it was called?

    This is not possible with the shutdown function, and you need to have your own custom_die()

  14. #14
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by RDFrame
    Damn, never mind. Seems as though register_shutdown_function() runs on a difference process, so a debug_backtrace() won't work on it. Any way of getting around that one?

    Right now at least, I just want something so when execution ends, a debug_backtrace() can be parsed and appended to a file. Any advice?

    Thanks,
    Matt
    Look at the auto_append_file php.ini setting (like someone already suggested). As I understand it, once execution is finished, the auto_append_file will be run. So any echoes will not appear in your page.

    Difference with register_shutdown_function is that register_shutdown_function is called after all output has been sent to the browser. With auto_append_file the connection to the browser is not yet closed, so any new output will appear in the browser too.

  15. #15
    SitePoint Addict evertcollab's Avatar
    Join Date
    Apr 2006
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was not part of the question (output or not)

    You can still write to files as the OP asks with register_shutdown_function() except that you can't use debug_backtrace (and get some usefull results)

  16. #16
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    Romania, Iasi
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A trick i've sometimes used is to create an object that has the code i need runing when the script finishes in object->__destruct().

    My 'normal' page-code is in objects that i destroy manually (i like to clean things up manually - an "old" habbit).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •