The characteristics of static methods in PHP are: 1. Static methods are modified with the static keyword; 2. Static methods can be used anywhere in the code; 3. Static methods can be accessed without the need for instance objects; 4. , Static methods can only operate static variables, not non-static variables.
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
Static methods are also called class methods. Static methods belong to all object instances: static function method name
Outside the class: class name: :Class method name or object name->Class method name
Inside the class: self::Class method name or class name::Class method name
If a variable requires all variables When sharing, you need to introduce static variables; if a method only operates static variables, use static methods
When operating static variables, you need to operate static methods. Non-static attributes (variables) cannot be accessed in static methods.
Characteristics of static methods:
1. Static methods can only operate static methods and cannot operate non-static variables
2. Ordinary member methods can operate non-static variables Variables can also operate static variables
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, and can use the static properties to set the value. The value can be used by all objects of the class;
(3) No instance object is required. Access static properties or methods.
Static methods cannot access ordinary properties in this class because those properties belong to an object, but static properties can be accessed.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the characteristics of php static methods. For more information, please follow other related articles on the PHP Chinese website!