This post covers the Static Methods and Properties section of the OOP chapter when studying for the Zend PHP 7 Certification.
Declaring class properties or methods as static, using the static
keyword, makes them accessible without needing an instantiation of the class.
A property declared as static cannot be accessed with an instantiated class object (though a static method can).
<?php
class MyClass {
public static $someVar = 'Some property';
public static function someMethod()
{
return __FUNCTION__;
}
}
echo MyClass::$someVar; // Outputs: Some property
echo MyClass::someMethod(); // Outputs: someMethod
<?php
class MyClass {
public static $someVar = 'Some property';
public static function someMethod()
{
return __FUNCTION__;
}
}
echo MyClass::$someVar; // Outputs: Some property
echo MyClass::someMethod(); // Outputs: someMethod
$myClass = new MyClass();
echo $myClass->someVar; // Generates an E_DEPRECATED warning (Property declared as static)
echo $myClass->someMethod(); // Outputs: someMethod
In PHP 7, as seen above, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED
warning. Be aware of this as current support for this may be removed in future.
Similarly to non-static properties and methods, if no visibility declaration is used, then the properties and methods are treated as public
.
It is possible to reference the class using a variable.
<?php
class MyClass {
public static $someVar = 'Some property';
public static function someMethod()
{
return __FUNCTION__;
}
}
$className = 'MyClass';
echo $className::$someVar; // Outputs: Some property
echo $className::someMethod(); // Outputs: someMethod
The self
keyword is used to access properties and methods from inside the class definition. self
refers to the current class.
<?php
class MyClass {
public static $someVar = 'Some property';
public static function someMethod()
{
echo self::$someVar . "\n";
echo self::someOtherMethod();
}
public static function someOtherMethod() {
return __FUNCTION__;
}
}
MyClass::someMethod();
// Outputs:
Some property
someOtherMethod
Note that because classes do not have be instantiated for access to static properties and methods, attempting to use the $this
pseudo-variable will result in a fatal error.
<?php
class MyClass {
public static $someVar = 'Some property';
public static function someMethod()
{
echo self::$someVar;
echo $this->someOtherMethod(); // Fatal error
}
public static function someOtherMethod() {
return __FUNCTION__;
}
}
MyClass::someMethod();
The parent
keyword refers to the parent of the current class.
<?php
class OtherClass extends MyClass
{
public static function someChildMethod() {
echo parent::someMethod();
}
}
There have been questions raised by developers about when to use static methods, and what benefit they have over non-static methods.
Static methods are perfectly acceptable to use when you’re defining methods that act in a stateless manner and are predictable.
Examples of static methods could include simple mathematical calculations for testing purposes. Logging classes and needing to output log messages to a file could be another reason to use static methods. Ut is easier to just write Logger::log()
for example, rather than instantiate a new class of Logger
each time.
View the other sections:
Note: This article is based on PHP version 7.0.