Re: destructor not called for static members?

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

 



Richard, OP,

Richard Lynch wrote:
Richard Lynch wrote:

<?php
class c1 {
 public function __destruct() {
   echo '__destruct';
 }
}

class c2 {
 private static $_ref;

 public function __construct() {
   self::$_ref = new c1();

   //$this->_ref =  new c1();
 }
}

$obj = new c2();
unset($obj);
?>


self::$_ref is completely independent of any instance of that class.


c2::$_ref has no value unless and until you create at least one (1) c2
instance.

That is the only dependency.

c2::$_ref cannot be destroyed by PHP unless and until the entire script is
dying because it is STATIC and if you create a second c2, it shouldn't

the _var_ ;-) is defined static at the class level (and also defined as private which means it happens to be only accessible from with the methods of the class its defined) 'static' changes its scope to the class iso the object in this situation.

change, right?

not quite,...

private static $_ref;

is only read once (obviously?) when parsed, but the
constructor of the class sets it to a new object on each
invocation, I can explain it best with code:

<?

class Test {
	public static $_ref = 1;
	
	// "static" in a function:
	static public function myFunc()
	{
		static $v = false;
		if ($v === false) {
			echo "init\n";
			$v = 0;
		}

		echo ++$v . "\n";
	}
}

echo Test::$_ref . "\n";
Test::$_ref++;
echo Test::$_ref . "\n";

Test::myFunc();
Test::myFunc();
Test::myFunc();

// I like static class vars (or whatever the proper name is)


So you only ever have, at most, one c1 running around.

You have no c1's until you create a c2.


in the OPs code - true, but there could be static method of the class to init the static class var.

Then you have 1 c1 until the script ends.

only if you don't unset it - which you can do - obviously the original snippet didn't. <not-particularly-for-richards-benefit> as always you can only (optionally) set a var to a literal when defining it as static. (i.e. in that statement), but afterwards you can set the 'static' to whatever. </not-particularly-for-richards-benefit>


At least, that's what I'd expect from a STATIC property based on other OO languages... Never actually used PHP's OO stuff for reasons we needn't repeat

<aural voice-type="pantomime-arch-villain"> I'm not finished with you yet m'laddie </aural>

;-)

seriously tho, php5 is really nice, I hear it calling to you :-)



-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[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