In PHP, member properties and member methods modified by the static keyword are called static properties and static methods. The syntax for accessing static properties and static methods is "class name::$static property" and "class name::staticmethod()".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, modified by the static keyword Member properties and member methods are called static properties and static methods. Here they can be collectively referred to as static members. Static members in a class are different from general members in the class. Static members will not be instantiated into objects, that is to say We can access static members through a class without instantiating a class.
It is easy to declare static variables in a class. We can add a static keyword before an ordinary member of the class to turn this ordinary member into a static member. In this way, we can directly access these static members in the class without instantiating the class. The syntax format for accessing static members is as follows:
类名::$静态属性 类名::静态方法()
The ::
symbol is called the range resolution operator, which is used to access static members, static methods and constants, and can also be used to override classes members and methods in .
If you want to access static properties in a member method inside a class, just add the operator self::
before the name of the static property.
The following is a simple example to demonstrate the definition of static members:
<?php header("Content-type:text/html;charset=utf-8"); class Website { static public $name = 'PHP中文网<br>'; public $title = 'PHP教程'; public static function url() { echo self::$name; echo 'https://www.php.cn/<br>'; echo $this -> title; } } Website::url(); ?>
The running results are as follows:
Note: In the static method , only static variables can be called, not ordinary variables; ordinary methods can call static variables.
Using static members, in addition to eliminating the need to instantiate the object, another function is to still save the modified static data after the object is destroyed so that it can be used next time. This is the same as what we introduced earlier Static variables are similar.
[Example] Declare a static variable $num, then declare a method, call the static variable inside the method and add 1 to the value of the variable; then instantiate the object in the class, and finally, call the class Methods.
<?php header("Content-type:text/html;charset=utf-8"); class Website { static public $num = 0; public function add() { echo '$num 的值为:' . self::$num; self::$num++; } } $num1 = new Website(); $num1 -> add(); echo '<br>'; $num2 = new Website(); $num2 -> add(); ?>
The running results are as follows:
#Static members do not need to instantiate objects. When the class is loaded for the first time, the memory space has been allocated, so directly Calling static members is faster. However, if too many static members are declared, the space will be occupied all the time, which will affect the running speed of the system.
Note:
Subclasses can override the static methods of the parent class.
You need to use the :: symbol to access static variables within a method. You cannot use $this;
Static methods and static variables will be inherited by subclasses.
Static variables cannot be accessed by ->
, but by ::
(double colon)
PHP Video Tutorial"
The above is the detailed content of What are static properties and static methods in php. For more information, please follow other related articles on the PHP Chinese website!