|
Posted by Jerry Stuckle on 11/15/06 20:26
Thomas Mlynarczyk wrote:
> Hi,
>
> I have this code:
>
> class Test
> {
> public $status = 'dead';
> function __construct() { $this->status = 'alive'; }
> function __destruct() { echo '<br>__destruct()'; }
> }
> $o = new Test;
>
> function shutdown()
> {
> echo '<br>shutdown()';
> }
> register_shutdown_function('shutdown');
>
> function obflush( $s )
> {
> global $o;
> return $s . '<br>obflush() ' . $o->status;
> }
> ob_start('obflush');
>
> Which (using PHP 5.1.4) produces this output:
>
> shutdown()
> __destruct()
> obflush() alive
>
> I have two questions:
>
> 1) I have read that the order in which the three functions are called has
> changed previously and is likely to change again in future versions of PHP.
> Does this mean I cannot rely on this order at all?
>
> 2) Why is $o still "alive" in obflush() even though its destructor has been
> called before? The destructor having been called, I would expect global $o
> to point to a no longer existing variable (thus, "null").
>
> Greetings,
> Thomas
>
>
It doesn't make any difference what order the functions are in - they
are only called when your code calls them directly or indirectly (i.e. a
constructor). So whatever order you call them in is the way they will
be called.
As for the variable - the object is destroyed, the destructor is called.
But that's not what's happening here. You are calling the destructor,
so it acts just like any other function.
Rather, the destructor is called implicitly during script termination or
when the variable goes out of scope. Like a constructor, you should not
be calling a destructor.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|