Re: [PHP] class (not class instance) variables

2001-05-06 Thread Gyozo Papp

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 
- [EMAIL PROTECTED]

- Original Message - 
From: Andrzej Swedrzynski [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Cc: Steven Haryanto [EMAIL PROTECTED]
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] class (not class instance) variables

2001-05-05 Thread Steven Haryanto

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?

--
sh 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] class (not class instance) variables

2001-05-05 Thread Andrzej Swedrzynski

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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]