This article mainly introduces the definition of the static keyword in PHP, late binding and the difference with the self keyword. Interested friends can refer to it. I hope it will be helpful to everyone.
Overview
I am learning design patterns. There was an article about the singleton pattern before. I re-read this article and found that I don’t have a good grasp of the static keyword. Reliable, review it again.
static keyword
The characteristics of static members and methods in PHP are as follows:
1. Static members cannot be accessed through instances of the class, but static The method is fine.
2. Static members cannot be accessed through the -> operator.
3. In the scope of a static method, the $this keyword cannot appear, which means that ordinary member variables cannot be accessed in a static method.
4. Static members and methods can be accessed directly through the class name without instantiating the object.
Late Static Bindings
Since PHP 5.3.0, PHP has added a feature called late static binding, which is used in inheritance scopes Internal reference to the statically called class.
To be precise, the working principle of late static binding is to store the class name in the previous "non-forwarding call" (non-forwarding call). When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator); when making a non-static method call, it is the class to which the object belongs. The so-called "forwarding call" refers to static calls made in the following ways: self::, parent::, static:: and forward_static_call(). You can use the get_called_class() function to get the class name of the called method, and static:: points out its scope.
To understand this feature, you can refer to the examples in the manual
self vs static
Use a demo to Directly explain the difference between self and static.
self example:
The code is as follows:
<?php class Vehicle { protected static $name = 'This is a Vehicle'; public static function what_vehicle() { echo get_called_class()."\n"; echo self::$name; } } class Sedan extends Vehicle { protected static $name = 'This is a Sedan'; } Sedan::what_vehicle();
Program output:
The code is as follows:
SedanThis is a Vehicle
static example:
The code is as follows:
<?php class Vehicle { protected static $name = 'This is a Vehicle'; public static function what_vehicle() { echo get_called_class()."\n"; echo static::$name; } } class Sedan extends Vehicle { protected static $name = 'This is a Sedan'; } Sedan::what_vehicle();
Program output:
The code is as follows:
Sedan This is a Sedan
Summary: Above That’s the entire content of this article, I hope it will be helpful to everyone’s study.
Related recommendations:
How to use the Snoopy class to parse html files
php simulates post Three common uses of requests
php definition and implementation of event mechanism
The above is the detailed content of The definition of the static keyword in PHP, late binding and the difference from the self keyword. For more information, please follow other related articles on the PHP Chinese website!