What do you need a global
declaration for in a class
property? We're not dealing with plain functions
here. $this->var1
will get you $var1
from any method
inside the class, or from an instantiated object outside the class, where the variable is public
. But let's be thorough...
Illustrating "Global"
Global has no meaning for class properties; although you can use it with variables inside class methods, just as you can do with regular functions. (But there are more elegant ways to get there; it's better to avoid a load of potentially hazardous clutter in the global scope.) Let's first define a variable:
$globby = 123; // Globby is a friend living outside a function.
A function that doesn't use a global
declaration can neither access a variable from outside of its scope, nor change that variable's value. The story goes like this:
function foo() {
echo $globby; // Here we have no clue who globby is?
$globby = 321; // Here we define globby, but then he's 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 both access it and modify it outside the function's scope. Also, a variable newly defined as global inside the function is accessible outside it.
function foot() {
global $globby; // Hey globby, won't you come on in! *Globby UFO Lands*
echo $globby; // - Who are you? - I'm globby that says 123.
$globby = 321; // - But I'm gonna tell you to say 321 everywhere.
}
foot(); // => '123' ... this here is the good old globby from the start.
echo $globby; // => '321' ... see, globby can change outside the function scope!
Illustrating "Static" in Classes
Be aware that static
for a class property doesn't work like static
for a function variable. Manual: "A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope." And again: "Declaring class properties or methods as static makes them accessible without needing an instantiation of the class." (OOP Static Keyword; and Using Static Variables) Class properties retain their value for the lifetime of the object in any case.
Now, to illustrate the usage (and non-usage) of static and non-static methods (aka. "class functions") and properties (aka. "class variables"). Let's make a little class:
class foo {
static $one = 1; // This is a static variable aka. class property.
var $two = 2; // But this is non-static.
static function say_one() { // This is a static method.
echo self::$one; // Uses self:: to statically access the property.
}
function say_two() { // This is a non-static method.
echo $this->two; // Uses $this-> to dynamically access the property.
}
}
Then let's see what works and what doesn't. Non-working options 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 that clarifies. Note that you can access a static method via an instantiated object, and use that to access a static variable; but you can't directly access static variables as non-static without warnings.
Let me add a note on good practice. If you find that you need to be repeatedly declaring global
variables inside your functions, or passing configuration parameters etc. as function arguments, it's a sign that you should probably be factoring your code into a class instead, accessing these globals as its properties. OOP is so much cleaner to work with. You'll code happier. $OOP->nirvana();
.