This article mainly introduces the classes and objects in PHP: Static properties, static methods, the need for friends can refer to the following
In PHP, using the keyword static modifier member properties and member methods are called static properties and static methods. static properties and static methods do not need to be used directly in cases where the class is instantiated.
Static properties
Unlike normal member properties, a static property belongs to the class itself and not to any instance of the class. Static properties can be seen as global variables stored in classes that can be accessed from anywhere through classes.
Because static properties are not constrained by any specific object, you do not need to establish a class instance, but you can directly reference a static property in the class, using the syntax format as follows:
Class Name:: $ static property name
Where the symbol "::" is called the scope-resolution operator, which can be used to access static members, static methods and constants, and to override members and methods in a class.
If you want to access a static property in a member method of the same class, you can do so by adding the operator "Self::" Before the name of the static property. Examples are as follows:
Class dabing{ static $bing =0; Define static properties Bing function AddOne () {self :: $bing + +; Call a static property in the same class Bing echo "\ $bing has the value:". Self:: $bing + +; }} $obj =new dabing (); $obj->addone ();
This program causes the value of the static property to be added 1, and the result is: 1
Static methods
Declaring a member method static in PHP is called a static method. Because static methods are not constrained by any specific object, you do not need to establish a class instance to directly reference a method in a class. The syntax format is as follows:
Class Name:: Static method Name ([parameter 1, Parameter 2, ...] )
The same as calling a static property, using the Operator "self::" In a class to represent the invocation of a static method in the same class, as in the following example:
Class dabing{static function AddOne ($number) {//define static method AddOne () echo "\ $number +1="; echo $number +1; } static function Showresult ($number) { echo "\ $number =". $number; echo "<br>"; Self::addone ($number); Call static methods in the same class AddOne () }} $number =100;dabing::showresult ($number);
The result of the program execution is:
$number =100
$number +1=101
The above is the whole content of this article, I hope that everyone's study has helped.