If you need an instance variable (stored only for this instance of the class), use:
$this->test
(as suggested by another answer).
If you want the variable "class", attach it with the keyword "static" as follows:
A class variable differs from an instance variable in that all instances of objects created from the class will have the same variable.
(Note to access class variables, use the Class Name or "I" followed by "::")
class myClass { public static $test = "The Test Worked!"; function example() { echo self::$test; } function example2() { echo self::$test." again"; } }
Finally, if you want a true constant (immutable), use 'const' in front (access it again with 'self' plus '::' plus the name of the constant (although this time skip '$'):
class myClass { const test = "The Test Worked!"; function example() { echo self::test; } function example2() { echo self::test." again"; } }
Matthew mucklo
source share