What are static properties and static methods in php

青灯夜游
Release: 2023-03-14 18:32:01
Original
3211 people have browsed it

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()".

What are static properties and static methods in php

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:

类名::$静态属性
类名::静态方法()
Copy after login

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 = &#39;PHP中文网<br>&#39;;
	public $title = &#39;PHP教程&#39;;
	public static function url() {
		echo self::$name;
		echo &#39;https://www.php.cn/<br>&#39;;
		echo $this -> title;
	}

}

Website::url();
?>
Copy after login

The running results are as follows:

What are static properties and static methods in php

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 &#39;$num 的值为:&#39; . self::$num;
		self::$num++;
	}

}

$num1 = new Website();
$num1 -> add();
echo &#39;<br>&#39;;
$num2 = new Website();
$num2 -> add();
?>
Copy after login

The running results are as follows:

What are static properties and static methods in php

#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)

  • ## objects You can directly call static member methods through the object name ->, which is the same as calling ordinary methods.

Recommended learning: "

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template