Static class properties or methods can be accessed without needing an instantiation of the class.
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
use static keyword to declare a static method.
if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
class Site {
visibility static function methodName() {
}
}
visibility: public,private,protect
To call a static method,we can use the Scope Resolution Operator (::).
inside the class: self::
outside the class: className::
access parent class's static method from child class: parent::
<?php
class Site {
public static function HostName() {
return "Lautturi<br>";
}
public function showHostName(){
// the visibility of HostName should be public
echo self::HostName();
}
}
class MySite extends Site{
public function showMySite(){
// the visibility of HostName should be public or protected
echo parent::HostName();
}
}
// the visibility of HostName should be public
echo Site::HostName();
$site = new Site();
$site -> showHostName();
$mysite = new MySite();
$mysite -> showHostName();
?>
Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed.
class Site {
public static $static_property = 'Lautturi';
}
we can use the Scope Resolution Operator (::) to access the static property.
inside the class: self::
outside the class: className::
access parent class's static property from child class: parent::
Static properties cannot be accessed through the object using the arrow operator ->. (use a non-static method or Scope Resolution Operator ::).
<?php
class Site
{
public static $my_static = 'Lautturi';
public function staticValue() {
return self::$my_static;
}
}
class MySite extends Site
{
public function siteStatic() {
return parent::$my_static;
}
}
echo Site::$my_static . "<br>";
$site = new Site();
echo $site->staticValue() . "<br>";
echo $site->my_static . "<br>"; // Undefined "Property" my_static
echo $site::$my_static . "<br>";
$classname = 'Site';
echo $classname::$my_static . "<br>"; // As of PHP 5.3.0
echo MySite::$my_static . "<br>";
$mysite= new MySite();
echo $mysite->siteStatic() . "<br>";
?>
PHP static methods are often used in Helper and Utility classes of PHP frameworks to provide additional support for classes.
If a logic/field that can be shared by multiple instances of the class, you can extract the code and put it into a static method.
<?php
class Counter{
private $count=0;
public static $instance=0;
public function __construct(){
self::$instance++;
}
public function count(){
$this->count++;
return $this;
}
public static function countInstance(){
return self::$instance;
}
public function getCount(){
return $this->count;
}
}
$counter1 = new Counter();
$counter1->count()
->count();
echo 'Count 1: '. $counter1->getCount() . '<br/>';
$counter2 = new Counter();
$counter2->count()
->count()
->count();
echo 'Count 2: '. $counter2->getCount() . '<br/>';
echo 'Instances number:' . Counter::countInstance() . '<br/>';
?>
Outputs:
Count 1: 2 Count 2: 3 Instances number:2