The advantages of static methods in php are: 1. Static methods can be used anywhere in the code; 2. Each instance of the class can access the static properties defined in the class; 3. No instance object is required You can access static properties or methods.
Advantages of static methods:
(Recommended tutorial: php graphic tutorial)
(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, and can use static properties to set values , this value can be used by all objects of the class;
(3) Static properties or methods can be accessed without instance objects.
Static methods cannot access ordinary properties in this class because those properties belong to an object, but static properties can be accessed.
(Video tutorial recommendation: php video tutorial)
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.
Static methods cannot be called in objects. Static methods and properties are also called class methods and class properties, so pseudo variables $this cannot be used in objects.
The above is the detailed content of What are the advantages of static methods in php. For more information, please follow other related articles on the PHP Chinese website!