Home > Backend Development > PHP Tutorial > PHP uses the static keyword to declare static properties and static methods. Sample code sharing

PHP uses the static keyword to declare static properties and static methods. Sample code sharing

黄舟
Release: 2023-03-12 12:08:01
Original
1867 people have browsed it

PHP uses the static keyword to declare static properties and static methods

In PHP, member properties and member methods modified by the static keyword are called Static properties and static methods.

Static properties and static methods can be used directly without being instantiated by a class.

1. Static attributes

Static attributes are member attributes modified with the static keyword. They belong to the class itself and not to any instance of the class. It is equivalent to a global variable stored in the class and can be accessed anywhere through the class.

Static properties cannot be accessed through an object whose class has been instantiated.

The syntax format of static property access is as follows:

类名称::$静态属性名称
Copy after login

The symbol "::" is called range resolutionOperator, used to access static properties and static methods and constants can also be used to override members and methods in a class.

Add the operator "self::" before the name of the static property to access the static property in the member method inside the class. By using only the class name::, you can call static properties inside the class from outside the class.

Note:

Static properties cannot be accessed by objects through the -> operator.

Like all other PHP static variables, static properties can only be initialized to literals or constants, and cannot use expressions. So a static property can be initialized to an integer or an array, but it cannot be initialized to another variable or function return value, nor can it point to an object.

2. Static method

Static method is a member method modified by the static keyword. Since it is not restricted by any object, you can directly reference the static methods in the class without instantiating the class.

Static methods can be accessed through an object of a class that has been instantiated.

The syntax format of static method reference is as follows:

类名称::静态方法名称([参数1,参数2,...])
Copy after login

Similarly, add the operator "self::" before the name of the static method to reference the static method in the member method inside the class method. By using only the class name::, you can call static methods inside the class from outside the class.

Note:

In static methods, only static variables can be called, not ordinary variables; ordinary methods can call static variables.

Calling a non-static method statically will result in an E_STRICT level error.

Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.


Tips

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 continue next time use.

In order to be compatible with PHP 4, if not specified as protected or private, static properties and static methods default to public.

Since PHP 5.3.0, you can use a variable to dynamically call a class, but the value of the variable cannot be the keywords self, parent, or static.


Explanation

Static members do not need to instantiate objects. Memory space has been allocated when the class is loaded for the first time, so calling static members directly is faster. However, if too many static members are declared, the space will be occupied all the time, which will affect the functionality of the system. This scale can only be truly grasped through accumulation of practice!


Example

<?php
class website{  static $num="0";                         // 定义静态变量
  public function visit(){                 // 定义visit方法
    echo "您是第".self::$num."位访客!";   // 输出静态变量信息
    self::$num++;                          // 静态变量做自增运算
  }
}

echo &#39;第一次实例化调用:<br />&#39;;
$website=new website();                    // 实例化对象
$website->visit();                         // 调用对象$website的visit()方法
$website->visit();
$website->visit();

echo &#39;<br />第二次实例化调用:<br />&#39;;
$website2=new website();                   // 改变对象句柄实例化对象
$website2->visit();
$website2->visit();
$website2->visit();
?>
Copy after login

View the display effect of PHP using the static keyword to declare static properties and static methods in the browser. As shown below:

The above is the detailed content of PHP uses the static keyword to declare static properties and static methods. Sample code sharing. 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