Home  >  Article  >  Backend Development  >  What does php static method mean?

What does php static method mean?

青灯夜游
青灯夜游Original
2020-07-22 13:44:122388browse

In PHP, static method refers to "static method"; member attributes and member methods modified by the static keyword are called static attributes and static methods; static methods cannot access ordinary attributes in this class , because those properties belong to an object, but static properties can be accessed.

What does php static method mean?

PHP static: static methods and attributes

In PHP, the member attributes and attributes modified by the static keyword Member methods are called static properties and static methods. They can be collectively called static members here. Static members in a class are different from general members in the class. Static members will not be instantiated into the object, which means we do not need By instantiating a class, you can access static members through the class.

[Related tutorial recommendation: "PHP Tutorial"]

It is easy to declare static variables in a class. We can add a static keyword before ordinary members of the class. , you can 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 and is used to access static members, static methods and constants. You can also use To override members and methods in a class.

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.

Static method

(1) Static methods cannot access ordinary properties in this class because those properties belong to an object, but static properties can be accessed;

(2) To access static methods or properties from the current class (not a subclass), you can use the self keyword, self points to the current class, just like $this points to the current object;

(3) Static methods cannot be called in objects. Static methods and attributes are also called class methods and class attributes, so the pseudo variable $this cannot be used in objects.

Advantages of static methods:

(1) Can be used anywhere in the code (assuming the class can be accessed);

(2 ) Each instance of the class can access the static properties defined in the class. You can use static properties to set values. The value can be used by all objects of the class;

(3) No instance object is required to access Static properties or methods.

Benefits and disadvantages of php static methods

1. The static method is a member method in the class and belongs to the entire class. It can be called directly without creating any objects. !

2. Static methods are more efficient than instantiation. The disadvantage of static methods is that they are not automatically destroyed, while instantiated methods can be destroyed.

3. Static methods and static variables always use the same memory after creation, while using instances will create multiple memories.

4. In C, if the static keyword is added before a class method, the method is called a static method, otherwise it is an instance method. Static methods are owned by the class and can be used through objects or classes. However, it is generally recommended to use it through the class name, because static methods can be used as long as the class is defined and there is no need to create an instance of the class. Static methods can only use static members of the class.

Recommended learning: PHP programming from entry to proficiency

The above is the detailed content of What does php static method mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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