Page 1 of 1
Does PHP have Class Variables (not instance variables)
Posted: Thu Mar 22, 2007 10:24 am
by Kadanis
A couple of years ago I touched on OOP using Smalltalk as part of a course. As I recall there was a distinct difference between class and instance variables.
Instance variables, are pretty much the standard variable type each instance having their own variables at initialisation, changes to them won't affect other instances variables.
Class variables, for those unsure, are variables that belong to the class and only hold 1 value for all instances of the class. Any instance that changes this value changes it for all instances.
Does anything like this exist in PHP and if so how do I use it. I did try searching a little but have only found what I think are instance variables.
Posted: Thu Mar 22, 2007 10:39 am
by Chris Corbyn
You're referring to static fields

PHP 5 has them, but PHP4 doesn't.
Code: Select all
class A {
public static $something = "foo";
public static function setSomething($x) {
self::$something = $x;
}
}
If you need "static final" you can't do:
Code: Select all
class A {
final public static $SOMETHING = "foo";
}
But you can do:
Code: Select all
class A {
const SOMETHING = "foo";
}
This has the same effect

Posted: Thu Mar 22, 2007 1:09 pm
by Kadanis
cool
thanks mate. exactly what I was looking for.
Posted: Thu Mar 22, 2007 2:46 pm
by AKA Panama Jack
I think you will find that even in PHP5 the static variable is only for the current object and it not a global variable amoung multiple objects of the same class. I don't think any version of PHP has global class variables where you have a variable that contains data that is shared by every instance of that class.
BTW, PHP 4 has static variables in classes. I know I use them in a number of PHP4 programs.

Posted: Thu Mar 22, 2007 3:18 pm
by Chris Corbyn
AKA Panama Jack wrote:I think you will find that even in PHP5 the static variable is only for the current object and it not a global variable amoung multiple objects of the same class. I don't think any version of PHP has global class variables where you have a variable that contains data that is shared by every instance of that class.
BTW, PHP 4 has static variables in classes. I know I use them in a number of PHP4 programs.

?
Code: Select all
<?php
class A {
public static $x = "foo";
public static function setX($x) {
self::$x = $x;
}
public function getStaticX() {
return self::$x;
}
}
$obj1 = new A();
$obj2 = new A();
echo $obj1->getStaticX(); //foo
A::setX("bar");
echo $obj1->getStaticX(); //bar
echo $obj2->getStaticX(); //bar
echo A::$x; //bar
Seems like a global to me
EDIT | How do you think Singletons work in PHP5?

Posted: Thu Mar 22, 2007 4:58 pm
by Luke
i don't think php 4 can have static members
Posted: Thu Mar 22, 2007 5:20 pm
by dreamscape
AKA Panama Jack wrote:BTW, PHP 4 has static variables in classes. I know I use them in a number of PHP4 programs.

No it doesn't. Prior to PHP 5, only functions and class methods could have static variables.
Posted: Thu Mar 22, 2007 5:30 pm
by Jenk
iirc, you can do..
Code: Select all
<?php
class Foo
{
var $staticFooBar = 'something';
}
echo Foo::$staticFooBar;
?>
So you do have them, you just have to be aware when instantiating.
Posted: Thu Mar 22, 2007 5:46 pm
by dreamscape
Jenk wrote:iirc, you can do..
The ability to call a class variable with the Scope Resolution Operator ( :: ) is not the same thing as the ability for the class variable to be static.
Plus, you totally cannot do that in PHP 4. You'll get a parse error.
But you can *sort of but not really* get class static variables in PHP 4 if you really must have them and cannot use PHP 5. I would only do this as an absolute last resort:
Code: Select all
<?php
class Foo
{
var $static_var;
function Foo()
{
static $static_var = 0;
$this->static_var =& $static_var;
}
function increment()
{
return ++$this->static_var;
}
}
$foo1 = new Foo();
$foo2 = new Foo();
echo $foo1->increment(), "\n"; // 1
echo $foo2->increment(), "\n"; // 2
echo $foo2->increment(), "\n"; // 3
echo $foo1->increment(), "\n"; // 4
echo $foo2->increment(), "\n"; // 5
Posted: Fri Mar 23, 2007 4:28 am
by Maugrim_The_Reaper
I think you will find that even in PHP5 the static variable is only for the current object and it not a global variable amoung multiple objects of the same class. I don't think any version of PHP has global class variables where you have a variable that contains data that is shared by every instance of that class.
A static is attached to a class, not an object - it's not possible to have multiple static variables of the same name on the same class. This might be a misunderstanding since you refer to a static by using the class name as the prefix, or within an object by using the "self" keyword. Class constants follow much the same pattern.