PHP Static Methods and Properties
Summary: in this tutorial, you will learn about PHP static methods and static properties and understand the differences between the $this and self .
Introduction to PHP static methods and properties
So far, you have learned how to define a class that consists of methods and properties. To use the methods and properties of the class, you create an object and access these methods and properties via the object.
Since these methods and properties are bound to an instance of the class, they are called instance methods and properties.
PHP allows you to access the methods and properties in the context of a class rather than an object. Such methods and properties are class methods and properties.
Class methods and class properties are called static methods and properties.
Static methods
To define a static method, you place the static keyword in front of the function keyword as follows:
class MyClass < public static function staticMethod() < >>
Code language: HTML, XML (xml)
Since a static method is bound to a class, not an individual instance of the class, you cannot access $this inside the method. However, you can access a special variable called self . The self variable means the current class.
The following shows how to call a static method from the inside of the class:
self::staticMethod(arguments);
Code language: PHP (php)
To call a static method from the outside of the class, you use the following syntax:
className::staticMethod(arguments)
Code language: JavaScript (javascript)
MyClass::staticMethod()
Code language: CSS (css)
The following example defines the HttpRequest class that has a static method uri() that returns the URI of the current HTTP request:
class HttpRequest < public static function uri(): string < return strtolower($_SERVER['REQUEST_URI']); > >
Code language: PHP (php)
The following calls the uri() static method of HttpRequest class:
echo HttpRequest::uri();
Code language: PHP (php)
If the current HTTP request is https://www.phptutorial.net/php-static-method/ , the uri() method will return /php-static-method/ .
Static properties
To define a static property, you also use the static keyword:
public static $staticProperty;
Code language: PHP (php)
class MyClass < public static $staticProperty; public static function staticMethod() < >>
Code language: PHP (php)
To access a public static property outside of the class, you also use the class name with the :: operator:
MyClass::$staticProperty;
Code language: PHP (php)
Like the static methods, to access static properties from within the class, you use the self instead of $this as follows:
self::staticProperty
Code language: PHP (php)
self vs. $this
The following table illustrates the differences between the self and $this :
$this | self |
---|---|
Represents an instance of the class or object | Represents a class |
Always begin with a dollar ($) sign | Never begin with a dollar( $ ) sign |
Is followed by the object operator ( -> ) | Is followed by the :: operator |
The property name after the object operator ( -> ) does not have the dollar ($) sign, e.g., $this->property . | The static property name after the :: operator always has the dollar ( $ ) sign. |
PHP static methods and properties example
Suppose that you want to create an App class for your web application. And the App class should have one and only one instance during the lifecycle of the application. In other words, the App should be a singleton.
The following illustrates how to define the App class by using the static methods and properties:
class App < private static $app = null; private function __construct() < >public static function get() : App < if (!self::$app) < self::$app = new App(); > return self::$app; > public function bootstrap(): void < echo 'App is bootstrapping. '; > >
Code language: HTML, XML (xml)
First, define a static property called $app and initialize its value to null :
private static $app = null;
Code language: PHP (php)
Second, make the constructor private so that the class cannot be instantiated from the outside:
private function __construct() Code language: PHP (php)
Third, define a static method called get() that returns an instance of the App class:
public static function get() : App < if (!self::$app) < self::$app = new App(); > return self::$app; >
Code language: PHP (php)
The get() method creates an instance of the App if it has not been created, otherwise, it just simply returns the App’s instance. Notice that the get() method uses the self to access the $app static property.
Fourth, the bootstrap() method is just for demonstration purposes. In practice, you can place the code that bootstraps the application in this method.
The following shows how to use the App class:
$app = App::get(); $app->bootstrap();
Code language: HTML, XML (xml)
In this code, we called the get() static method from the App class and invoked the bootstrap() method of the App ‘s instance.
Summary
- Static methods and properties are bound to a class, not individual objects of the class.
- Use the static keyword to define static methods and properties.
- Use the self keyword to access static methods and properties within the class.
ЗлостныйКодер
У каждого обычно возникает вопрос, что такое $this, что такое self, для чего они используются и в чем разница между ними?
ПРАВИЛА КОТОРЫЕ ВЫ ДОЛЖНЫ ЗАПОМНИТЬ:
- Статические функции должны использовать только статические переменные.
- self ТОЛЬКО для статических функций, свойств. Но также можно вызвать нестатический метод, КАК СТАТИЧЕСКИЙ через self. Но лучше так не делать, а то папа побьет.
- this ТОЛЬКО для нестатических.
- this требует, чтобы класс был проинстанцирован, self не требует.
public function klassFunc() <>; function getName() echo $name; // неправильно, обратиться не к переменной public name echo $this->name; // правильно //вызов метода класса $this->klassFunc(); > >
Обычна нужна, чтобы инициализировать поля в конструкторе, ну и не только:
public function __construct($name)$name = $name; //будет ошибка$self используется в том, же самом ключе, но уже для СТАТИЧЕСКИХ свойств:
class human static $name = "ololo";public function klassFunc() <>; function getName() echo self::$name; //обращение к nameecho $this->name; //null или ошибка. НЕЛЬЗЯ > >Проверочная программа которая показывает главную разницу между self и $this:
class Klass const STAT = 'S' ; // константы всегда статичны static $stat = 'Static' ;
public $publ = 'Public' ;
private $priv = 'Private' ;
protected $prot = 'Protected' ;public function show ( ) print '
self::STAT: ' . self :: STAT ; // ссылается на константу
print '
self::$stat: ' . self :: $stat ; // статическая переменная.
print '
$this->stat: ' . $this -> stat ; // Ошибки нет, но выведет пустую строку. ПРЕДУПРЕЖДЕНИЕ.
print '
$this->publ: ' . $this -> publ ; // выведет как надо
print '
' ;
>
> $me = new Klass () ; $me -> show () ;Результат:
self::STAT: S
self::$stat: Static
Strict Standards: Accessing static property Klass::$stat as non static in htdocs\test\testReference.php on line ..
Notice: Undefined property: Klass::$stat in C:\xampp\htdocs\test\testReference.php on line 50
$this->stat:
$this->publ: Public
Видно, что php умный и кидает предупреждения глупым программистам. Не делайте так.Второе главное отличие состоит в том, что
self не использует таблицу виртуальных методов, то есть:
(взято с StackOverflow)class Person private $name; public function __construct($name) $this->name = $name; > public function getName() return $this->name; > public function getTitle() return $this->getName()." the person"; > public function sayHello() echo "Hello, I'm ".$this->getTitle()."
"; > public function sayGoodbye() echo "Goodbye from ".self::getTitle()."
"; > > class Geek extends Person public function __construct($name) parent::__construct($name); > public function getTitle() return $this->getName()." the geek"; > > $geekObj = new Geek("Ludwig"); $geekObj->sayHello(); $geekObj->sayGoodbye();Hello, I'm Ludwig the geek
Goodbye from Ludwig the personПояснение:
Метод sayHello использует this, поэтому виртуальная таблица вызовет Geek::getTitle()
Метод sayGoodbye использует self, поэтому виртуальная таблица будет не задействована и будет вызван parent::getTitle()Решение:
Используем вместо self, static keyword:
public function sayGoodbye() echo "Goodbye from ". static::getTitle() ;