SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __destruct and shutdown functions not working

    Ok, here is the problem.

    I have a server with PHP 5.0.3, running on 2.6.11-gentoo, let's call it server A, and another one with 5.0.4, running on 2.6.10-gentoo-r2, server B.

    Here is the code that I run on both:
    PHP Code:
    <?php
    // set error displaying to full
    error_reporting(E_ALL E_STRICT);
    ini_set('display_errors'true);

    function 
    OnShutdown()
    {
      echo 
    "OnShutdown function called.\n";
    }

    // register this function to be run at script shutdown
    register_shutdown_function('OnShutdown');

    // define a class with a destructor
    // taken from php.net example for __destruct
    class MyDestructableClass
    {
      function 
    __construct()
      {
        print 
    "In constructor\n";
        
    $this->name "MyDestructableClass";
      }
      
      function 
    __destruct()
      {
        print 
    "Destroying " $this->name "\n";
      }
    }

    // create a class
    $obj = new MyDestructableClass();
    ?>
    The output is as follows:
    Server A:
    Code:
    In constructor
    OnShutdown function called.
    Destroying MyDestructableClass
    Server B:
    Code:
    In constructor
    So, before I panic, what should I check? Is there some setting to disable shutdown functions and destructors? I've found some bugs related to __destruct not called, but it seems all have been fixed quite a while ago, and it works with 5.0.3, but not 5.0.4.

    The thing is driving me crazy, specially because some of my new code is using some sort of automated database features with objects inserting/updating/deleting on destruction and I really need this to work.

    HELP!

  2. #2
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anybody (possibly with PHP 5.0.4) please test the given code and verify my results?

  3. #3
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there ANYONE willing to try the code???

  4. #4
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello?!

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please have patience.

    PHP 5.0.4

    Code:
    In constructor
     OnShutdown function called.
     Destroying MyDestructableClass
    My name is Steve, and I'm a super-villian.

  6. #6
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot. What system are you using?

    Patience is what I don't have - I'm in a hurry and not on this forum where I don't get ANY thread answered.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your shutdown functions are being called and they printed what you want. The reason why you don't see an ouput in your browser is that you're using gzip there and php shutdown is performed after gzipping.

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well ... not really. I actually discovered this problem when my automated objects werent saving data to database upon destruction. This printing test is just a minimal (non)working example of my problem.

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ----- HTTP Client Request -----
    
    GET /iass/lib/App/test_destruct.php HTTP/1.1 
    Host: www.kje.si
    
    ----- HTTP Server Reply -----
    
    * 1A 34 E5 B2 | 00 00 48 54 | 54 50 2F 31 | 2E 31 20 32 [.4....HTTP/1.1 2]
    * 30 30 20 4F | 4B 0D 0A 44 | 61 74 65 3A | 20 57 65 64 [00 OK..Date: Wed]
    * 2C 20 31 35 | 20 4A 75 6E | 20 32 30 30 | 35 20 31 32 [, 15 Jun 2005 12]
    * 3A 30 34 3A | 32 30 20 47 | 4D 54 0D 0A | 53 65 72 76 [:04:20 GMT..Serv]
    * 65 72 3A 20 | 41 70 61 63 | 68 65 0D 0A | 58 2D 50 6F [er: Apache..X-Po]
    * 77 65 72 65 | 64 2D 42 79 | 3A 20 50 48 | 50 2F 35 2E [wered-By: PHP/5.]
    * 30 2E 34 0D | 0A 43 6F 6E | 74 65 6E 74 | 2D 45 6E 63 [0.4..Content-Enc]
    * 6F 64 69 6E | 67 3A 20 67 | 7A 69 70 0D | 0A 56 61 72 [oding: gzip..Var]
    * 79 3A 20 41 | 63 63 65 70 | 74 2D 45 6E | 63 6F 64 69 [y: Accept-Encodi]
    * 6E 67 0D 0A | 43 6F 6E 74 | 65 6E 74 2D | 4C 65 6E 67 [ng..Content-Leng]
    * 74 68 3A 20 | 31 30 30 0D | 0A 4B 65 65 | 70 2D 41 6C [th: 100..Keep-Al]
    * 69 76 65 3A | 20 74 69 6D | 65 6F 75 74 | 3D 31 35 2C [ive: timeout=15,]
    * 20 6D 61 78 | 3D 31 30 30 | 0D 0A 43 6F | 6E 6E 65 63 [ max=100..Connec]
    * 74 69 6F 6E | 3A 20 4B 65 | 65 70 2D 41 | 6C 69 76 65 [tion: Keep-Alive]
    * 0D 0A 43 6F | 6E 74 65 6E | 74 2D 54 79 | 70 65 3A 20 [..Content-Type: ]
    * 74 65 78 74 | 2F 68 74 6D | 6C 3B 20 63 | 68 61 72 73 [text/html; chars]
    * 65 74 3D 49 | 53 4F 2D 38 | 38 35 39 2D | 32 0D 0A 0D [et=ISO-8859-2...]
    * 0A 1F 8B 08 | 00 00 00 00 | 00 00 03 F2 | CC 53 48 CE [.............SH.]
    * CF 2B 2E 29 | 2A 4D 2E C9 | 2F E2 02 00 | 00 00 FF FF [.+.)*M../.......]
    * 03 00 47 6F | 18 89 0F 00 | 00 00 4F 6E | 53 68 75 74 [..Go......OnShut]
    * 64 6F 77 6E | 20 66 75 6E | 63 74 69 6F | 6E 20 63 61 [down function ca]
    * 6C 6C 65 64 | 2E 0A 44 65 | 73 74 72 6F | 79 69 6E 67 [lled..Destroying]
    * 20 4D 79 44 | 65 73 74 72 | 75 63 74 61 | 62 6C 65 43 [ MyDestructableC]
    * 6C 61 73 73 | 0A          |             |             [lass.]
    As you can see, you have gzipped content, followed by your test prints. No browser (with surprising exception of Opera) is able to interpret this correctly.

  10. #10
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Your shutdown functions are being called and they printed what you want. The reason why you don't see an ouput in your browser is that you're using gzip there and php shutdown is performed after gzipping.
    Mighty good observation there, stereofrog: rep++!
    My name is Steve, and I'm a super-villian.

  11. #11
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, many thanks, stereofrog!!! If there was still rep on this forum, I'd hack it and give you 1024 points!

    So, because of the gzip compression, the output called from the shutdown functions (and destructor, because it's run at the end of the script in this case) doesn't get compressed along with all other response? Not that I really need to, but is it possible to output text via shutdown functions and gzip somehow?

    Still have to figure out why my UnitOfWork doesn't store anything in the database upon destruction, even though it was tested and was working on other servers ...

    Thanks again and best regards,
    fat

    Edit:


    With what tool did you read that? I got some listener installed on my computer, but its output is pretty much unreadable ...

  12. #12
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jpleveille at webgraphe dot com @ php.net/ref.zlib
    register_shutdown_function() (http://www.php.net/register_shutdown_function) won't output anything if you use zlib.output_compression.
    Shutdown function is called after closing all opened output buffers thus, for example, its output will not be compressed if zlib.output_compression is enabled.
    So, no chance

    The tool is called analyzer (http://analyzer.polito.it/). Not perfect, but the best I've seen (and it's free).

  13. #13
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. Well, outputing text at shutdown is something I can live without, although I imagine there could be some use for it. Perhaps for some framework that displays the whole view at the actual finish.

    Thanks again, also for the tool, which looks quite nice. In the first 5 minutes I didn't have any success trying to use it - have to read some docs.
    Last edited by dbevfat; Jun 15, 2005 at 13:53.


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
  •