Why do you need a global declaration for a class property? We are not dealing with the usual functions here. $this->var1 will get you $var1 from either method inside the class or from an object instance outside the class where the public variable. But let it be thorough ...
Illustration "Global"
Global does not matter for class properties; although you can use it with variables inside class methods, just like you can do with regular functions. (But there are more elegant ways to get there, it is better to avoid loading a potentially dangerous mess in a global area.) First, define a variable:
$globby = 123;
A function that does not use the global declaration cannot access the variable from outside its scope or change the value of the variable. The story is as follows:
function foo() { echo $globby; // Here we have no clue who globby is? $globby = 321; // Here we define globby, but then he totally forgotten. :( } foo(); // => 'Notice: Undefined variable: globby' echo $globby; // => '123' ... still the same old globby.
However, a function that declares a variable as global can access it and modify it outside the scope. In addition, a variable defined as global inside a function is available outside of it.
function foot() { global $globby;
The designation "Static" in classes
Remember that static for a class property does not work as static for a function variable. Manual: "A static variable exists only in the area of ββa local function, but it does not lose its value when the program execution leaves this area." And again: "Declaring class properties or methods as static makes them available without having to instantiate the class." ( OOP Static Keyword and Using Static Variables ) In any case, class properties retain their value for the lifetime of an object.
Now, to illustrate the use (and non-use) of static and non-static methods (the so-called "class functions") and properties ("class variables"). Let's make a small class:
class foo { static $one = 1;
Then let's see what works and what doesnβt. Inactive parameters are commented out.
/* Static Variables and Methods */ echo foo::$one; // => '1' echo foo::say_one(); // => '1' // echo foo::$two; // => "Fatal error: Access to undeclared static property: foo::$two" // echo foo::say_two(); // => "Strict: Non-static method foo::say_two() should not be called statically. // & Fatal error: Using $this when not in object context." /* Non-Static Variables and Methods */ $f = new foo(); // This here is a real effin' instantiated dynamite object. *BOOM* echo $f->two; // => '2' echo $f->say_two(); // => '2' // echo $f->one; // => "Strict: Accessing static property foo::$one as non static. // & Notice: Undefined property: foo::$one." echo $f->say_one(); // => '1'
Hope this clears up. Note that you can access the static method through an instance of the object and use it to access the static variable; but you cannot directly refer to static variables as non-static without warnings.
Let me add a note about good practice. If you find that you need to repeatedly declare global variables inside your functions or pass configuration parameters, etc. As a function argument, this is a sign that you should probably factor your code into a class instead, by accessing these global variables as your properties, OOP is so much cleaner to work with. You will make the code happier. $OOP->nirvana(); .