Home  >  Article  >  Backend Development  >  What is the difference between static methods and non-static methods in php?

What is the difference between static methods and non-static methods in php?

青灯夜游
青灯夜游Original
2020-11-05 15:56:054837browse

Difference: 1. Non-static methods can access any member in the class, while static methods can only access static members in the class; 2. Static methods are already loaded and allocated when the class is defined, and non-static methods Methods do not occupy memory when they are defined, and memory is allocated only when they are instantiated as objects.

What is the difference between static methods and non-static methods in php?

Recommended: "PHP Video Tutorial"

Static methods and non-static methods in php The difference between static methods

1. Static methods belong to the class and can be used before the class is instantiated. That is, you can use the object's methods without newing an object. For example, in the format of object::fun1();

2. Non-static methods can access any member in the class. Static methods can only access static members in the class. Non-static members cannot be called, but they can Adjust non-static methods (because the system automatically converts non-static methods into static methods)

3. Static methods are already loaded and allocated when the class is defined. Non-static methods do not occupy memory when they are defined, only Memory will be allocated when instantiated as an object

4. Only static variables and other static methods can appear inside static (that is, static methods cannot call non-static properties. Self:: cannot be used to call non-static properties) !And keywords such as this cannot be used in the static method, because it belongs to the entire class. To call other static methods within a static method, you can use the form of self:: method name;

5. Static methods are more efficient than instantiation. The disadvantage of static methods is that they are not automatically destroyed, while instances The ones that are modified can be destroyed;

6. Static methods and static variables always use the same memory after they are created, while using instances will create multiple memories.

Main difference:

Static methods can be used before creating the object, and non-static methods must be called through the object produced by new.

There is no obvious difference in performance and memory usage between static methods and instance methods. Whether to declare a static method needs to be considered from the three aspects of the type's non-static fields, events, object-oriented extensions and polymorphism.

Summary: The static one has only one result no matter how many times it is instantiated, while the dynamic one has different results every time.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of What is the difference between static methods and non-static methods in php?. 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