PHP supports declaring methods as static.
Whatthis means is that your static methods are part of the
class and are not bound to any specific object instance and
its properties. Therefore, $this isn’t accessible in these methods,
but the class itself is by using selfto access it. Because
static methods aren’t bound to any specific object, you can
call them without creating an object instance by using the
class_name::method()syntax. You may also call them from an
object instance using $this->method(), but $thiswon’t
be defined in the called method. For clarity, you should use self::method()
instead of $this->method().
class PrettyPrinter {
static function printHelloWorld()
{
print "Hello, World";
self::printNewline();
}
static function printNewline()
{
print "\n";
}
}
PrettyPrinter::printHelloWorld();
The example prints the string "Hello, World"followed by a newline.
Although it is a useless example, you can see that printHelloWorld()can be
called on the class without creating an object instance using the class name,
and the static method itself can call another static method of the
class printNewline()using the self::notation. You may call a parent’s
static method by using the parent::notation.
class propertiesas opposed to object or instance properties. You can also
think of static properties as global variables that sit inside a class but are
accessible from anywhere via the class.
Static properties are defined by using the static keyword:
class MyClass {
static $myStaticVariable;
static $myInitializedStaticVariable = 0;
}
To access static properties, you have to qualify the property name with
the class it sits in
MyClass::$myInitializedStaticVariable++;
print MyClass::$myInitializedStaticVariable;
Whatthis means is that your static methods are part of the
class and are not bound to any specific object instance and
its properties. Therefore, $this isn’t accessible in these methods,
but the class itself is by using selfto access it. Because
static methods aren’t bound to any specific object, you can
call them without creating an object instance by using the
class_name::method()syntax. You may also call them from an
object instance using $this->method(), but $thiswon’t
be defined in the called method. For clarity, you should use self::method()
instead of $this->method().
class PrettyPrinter {
static function printHelloWorld()
{
print "Hello, World";
self::printNewline();
}
static function printNewline()
{
print "\n";
}
}
PrettyPrinter::printHelloWorld();
The example prints the string "Hello, World"followed by a newline.
Although it is a useless example, you can see that printHelloWorld()can be
called on the class without creating an object instance using the class name,
and the static method itself can call another static method of the
class printNewline()using the self::notation. You may call a parent’s
static method by using the parent::notation.
Static Properties
As you know by now, classes can declare properties. Each instance of the
class,object has its own copy of these properties. However, a class can
also contain static properties. Unlike regular properties, these belong to
the class itself and not to any instance of it.
class,object has its own copy of these properties. However, a class can
also contain static properties. Unlike regular properties, these belong to
the class itself and not to any instance of it.
class propertiesas opposed to object or instance properties. You can also
think of static properties as global variables that sit inside a class but are
accessible from anywhere via the class.
Static properties are defined by using the static keyword:
class MyClass {
static $myStaticVariable;
static $myInitializedStaticVariable = 0;
}
To access static properties, you have to qualify the property name with
the class it sits in
MyClass::$myInitializedStaticVariable++;
print MyClass::$myInitializedStaticVariable;