Does PHP have Class Variables (not instance variables)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Does PHP have Class Variables (not instance variables)

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

cool

thanks mate. exactly what I was looking for.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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? ;)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

i don't think php 4 can have static members
User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
Post Reply