Re: __destruct() not called ! we shot us in the foot try the script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



__destruct is called only when the object is detroyed.
The object is destroyed only when there is no references to it.


<?php

error_reporting( E_ALL | E_STRICT );
header( 'Content-Type: text/plain' );

class Foo
{
    function __destruct()
    {
        echo __METHOD__, PHP_EOL;
    }
}

$a = new Foo;
$b = $a; # <<<< try the script without this line or commented
unset( $a ); # no destructor called
echo '--', PHP_EOL;
unset( $b ); # destructor called!


On Mon, Aug 24, 2009 at 8:40 PM, Ralph Deffke <ralph_deffke@xxxxxxxx> wrote:

>
> well I would call this an error in the first view , and some of u where
> right! and the stuff with the refernce counter seems to be right as well.
>
> however I can't see a reason for it as 5.x works through refernces. so
> unsetting a REFERENCE to the object does not destroy it.
>
> How to destroy the object then?
>
> <?php
>
>
> abstract class a {
>  public function __construct(){
>    echo "constructing....<br>";
>  }
>  public function __destruct(){
>    echo "destructing....<br>";
>  }
> }
>
> class b extends a{
>
>  public function doSomething(){
>    echo "I'm doing ...but the reference c to the object is unset()<br>";
>  }
>
> }
>
> $c = new b();
>
> $d = $c ;   // works
> $f[] = $c ; // works
>
> class e {
>  public static $m;
>
>  public static function setM( $m ){
>    self::$m = $m;
>  }
> }
>
> $o = new e();
> e::setM( $c ); // works
>
> echo "unsetting ...<br>";
> unset( $c );
>
> $d->doSomething();
>
> echo "script ending now ...<br>";
>
> ?>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Martin Scotta

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux