![]() ![]() | |
To specify that a property of a class is static, you use the static modifier, as shown below.
static var variableName;
You can also declare methods of a class to be static.
static function functionName() {
// function body
}
Class (static) methods can access only class (static) properties, not instance properties. For example, the following code will result in a compiler error, because the class method getName() references the instance variable name.
class StaticTest {
var name="Ted";
static function getName() {
var local_name = name;
// Error! Instance variables cannot be accessed in static functions.
}
}
To solve this problem, you could either make the method an instance method or make the variable a class variable.
![]() ![]() | |