What you want is a static member
class MyClass {
public static $MyStaticMember = 0;
public function echoStaticMember() {
echo MyClass::$MyStaticMember;
//note you can use self instead of the class name when inside the class
echo self::$MyStaticMember;
}
public function incrementStaticMember() {
self::$MyStaticMember++;
}
}
then you access it like
MyClass::$MyStaticMember = "Some value"; //Note you use the $ with the variable name
Now any instances and everything will see the same value for whatever the static member is set to so take for instance the following
function SomeMethodInAFarFarAwayScript() {
echo MyClass::$MyStaticMember;
}
...
MyClass::$MyStaticMember++; //$MyStaticMember now is: 1
$firstClassInstance = new MyClass();
echo MyClass::$MyStaticMember; //will echo: 1
$firstClassInstance->echoStaticMember(); //will echo: 1
$secondInstance = new MyClass();
$secondInstance->incrementStaticMember(); // $MyStaticMember will now be: 2
echo MyClass::$MyStaticMember; //will echo: 2
$firstClassInstance->echoStaticMember(); //will echo: 2
$secondInstance->echoStaticMember(); //will echo: 2
SomeMethodInAFarFarAwayScript(); //will echo: 2
PHPFiddle