Re: unset( $anobject) does not invoce __destruct()
am 24.08.2009 16:49:01 von Stut
2009/8/24 Ralph Deffke :
> this is also not the full truth try this and it works
> what are the circumstances that is causing this problem then, yes I do ha=
ve
> distributed references over my script and there are clearly references st=
ill
> set, however after running the snipped script I can not see what I do
> special in my script causing the problem. I even tried with public and
> private static in other objects. it works. however the manual indicates t=
he
> refernce counter has to be 0.
Assuming you're using PHP 5...
>
>
>
> abstract class a {
> Â public function __construct(){
> Â Â echo "constructing....
";
> Â }
> Â public function __destruct(){
> Â Â echo "destructing....
";
> Â }
> }
>
> class b extends a{
>
> }
>
> $c =3D new b();
refcount =3D 1
> $d =3D $c ; Â // works
refcount =3D 2
> $f[] =3D $c ; // works
refcount =3D 3
> class e {
> Â private $m;
>
> Â public function setM( $m ){
> Â Â $this->m =3D $m;
> Â }
> }
>
> $o =3D new e();
> $o->setM( $c ); // works
refcount =3D 4 (due to assignment in setM)
> unset( $c );
refcount =3D 3
In PHP 5 all objects are passed by reference unless explicitly cloned.
This means that assigning an object variable to another variable does
nothing more than assign a reference and increment the referece count.
What exactly in the manual leads you to believe that after the unset
the refcount should be 0?
-Stuart
--=20
http://stut.net/
> "Lupus Michaelis" wrote in message
> news:41.F9.03363.011929A4@pb1.pair.com...
>> kranthi wrote:
>> > unset($obj) always calls the __destruct() function of the class.
>>
>> Â Â Never calls the dtor. The dtor will be called only when the=
reference
>> count reaches 0.
>>
>> class c { function __destruct() { echo 'dying !' ; } }
>> $v1 =3D new c ;
>> $v2 =3D $v1 ;
>>
>> unset($v1) ; // don't call the dtor
>> unset($v2) ; // call the dtor
>>
>> --
>> Mickaël Wolff aka Lupus Michaelis
>> http://lupusmic.org
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: unset( $anobject) does not invoce __destruct()
am 24.08.2009 21:48:16 von Ralph Deffke
Stuart, u are right, the refcount in php 5 doesn't matter, where something
left behind in my memory from earlier days, However i do have the effect
that unsetting an object does NOT call __dectruct() ! but when the script
ends it is called. this can be easily tested by putting a echo in destruct.
my objects affected are pretty much the sheme I send. however I'm doing some
reflection stuff in my classes. may be thats the reason. I will do some
further investigation about that.
"Stuart" wrote in message
news:a5f019de0908240749l8fa749s825cfa0e475f7775@mail.gmail.c om...
2009/8/24 Ralph Deffke :
> this is also not the full truth try this and it works
> what are the circumstances that is causing this problem then, yes I do
have
> distributed references over my script and there are clearly references
still
> set, however after running the snipped script I can not see what I do
> special in my script causing the problem. I even tried with public and
> private static in other objects. it works. however the manual indicates
the
> refernce counter has to be 0.
Assuming you're using PHP 5...
>
>
>
> abstract class a {
> public function __construct(){
> echo "constructing....
";
> }
> public function __destruct(){
> echo "destructing....
";
> }
> }
>
> class b extends a{
>
> }
>
> $c = new b();
refcount = 1
> $d = $c ; // works
refcount = 2
> $f[] = $c ; // works
refcount = 3
> class e {
> private $m;
>
> public function setM( $m ){
> $this->m = $m;
> }
> }
>
> $o = new e();
> $o->setM( $c ); // works
refcount = 4 (due to assignment in setM)
> unset( $c );
refcount = 3
In PHP 5 all objects are passed by reference unless explicitly cloned.
This means that assigning an object variable to another variable does
nothing more than assign a reference and increment the referece count.
What exactly in the manual leads you to believe that after the unset
the refcount should be 0?
-Stuart
--
http://stut.net/
> "Lupus Michaelis" wrote in message
> news:41.F9.03363.011929A4@pb1.pair.com...
>> kranthi wrote:
>> > unset($obj) always calls the __destruct() function of the class.
>>
>> Never calls the dtor. The dtor will be called only when the reference
>> count reaches 0.
>>
>> class c { function __destruct() { echo 'dying !' ; } }
>> $v1 = new c ;
>> $v2 = $v1 ;
>>
>> unset($v1) ; // don't call the dtor
>> unset($v2) ; // call the dtor
>>
>> --
>> Mickaël Wolff aka Lupus Michaelis
>> http://lupusmic.org
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php