Re: class (not class instance) variables

From: Date: Sun, 06 May 2001 11:46:18 +0000
Subject: Re: class (not class instance) variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-51605@lists.php.net to get a copy of this message
Hello, I don't know the way to declare static properties explicitly, maybe this is even not supported yet. But, ... when I recently develop some classes in PHP I've found that static variables declared inside methods behave like static properties in C++. This means you should write a function (more accurate: a method) like test() in the code below and declare your statics within this functions. My simple test() return the current value of the static variable or sets its new value if any parameter is given. I don't know this is the normal behaviour, but it works for me on PHP 4.0.4pl1 with Apache on Win98. The example is following: <?php class A { var $a; // static var $common; function A($a) { $this->a = $a; $this->test($a); } function test($set = '') { static $common; return empty($set) ? $common : $common = $set; } } $a1 = new A(4); // sets $common in test to 4. var_dump($a1->test()); $a2 = new A(5); // sets $common in test to 5. var_dump($a2->test()); // echoes "int(5)" var_dump($a1->test()); // echoes "int(5)" for $a1 too! ?> I hope this helps and please inform me if it doesn't return the expected result. Then I won't use this feature (in other occassions), so please be so kind ! best Papp Gyozo - pgerzson@freestart.hu ----- Original Message ----- From: "Andrzej Swedrzynski" <kokosz@earthdawn.pl> To: <php-general@lists.php.net> Cc: "Steven Haryanto" <steven@haryan.to> Sent: 2001. május 5. 22:36 Subject: Re: [PHP] class (not class instance) variables On Sun, 6 May 2001, Steven Haryanto wrote: > Am I correct that PHP does not support class variables? That is, > variables that belong to a class and not copied into every object. > I need to put several arrays for information about a class, and I > do not want to bloat every object with this data (since potentially > I will create many instances of the class). > > Currently I do this with methods. Any other alternative? For every class I do create additional class called MetaXXX where XXX is the name of the original class. In the MetaXXX I do decla­ re all variables and methods which I would make static in other language such as C++ or Java. Then I do create one and only one object of class MetaXXX (called metaXXX for convenience) which is available in global scope. Hope this will help you. Regards, Andrzej -- http://kokosz.horyzont.net http://www.earthdawn.pl -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#51605) next »