Does PHP have Class Variables (not instance variables)
Moderator: General Moderators
Does PHP have Class Variables (not instance variables)
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You're referring to static fields
PHP 5 has them, but PHP4 doesn't.
If you need "static final" you can't do:
But you can do:
This has the same effect 
Code: Select all
class A {
public static $something = "foo";
public static function setSomething($x) {
self::$something = $x;
}
}Code: Select all
class A {
final public static $SOMETHING = "foo";
}Code: Select all
class A {
const SOMETHING = "foo";
}- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
BTW, PHP 4 has static variables in classes. I know I use them in a number of PHP4 programs.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
?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; //barEDIT | How do you think Singletons work in PHP5?
- dreamscape
- Forum Commoner
- Posts: 87
- Joined: Wed Jun 08, 2005 10:06 am
- Contact:
iirc, you can do..
So you do have them, you just have to be aware when instantiating.
Code: Select all
<?php
class Foo
{
var $staticFooBar = 'something';
}
echo Foo::$staticFooBar;
?>- dreamscape
- Forum Commoner
- Posts: 87
- Joined: Wed Jun 08, 2005 10:06 am
- Contact:
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.Jenk wrote:iirc, you can do..
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- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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.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.