Difference: Static methods can be called directly through "class name::method name"; while ordinary methods need to create an instance, that is, a new object, and then call it through "object name->method name" to call. Static methods can only access static members of the class; ordinary methods can access any member of the class.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The static memory space is fixed, which is relatively more resource-saving.
Creating an instance requires opening up a new memory. Static methods that consume resources belong to the class and can be used before the class is instantiated;
Static methods can only access static members in the class;
Only static variables and other static methods can appear inside static! And keywords such as this cannot be used in static methods. Because it belongs to the entire class;
Static methods and static variables always use the same memory after they are created, while using instances will create multiple memories.
can access any member in the class, because static methods can be used before the class is instantiated, and the Non-static variables must be instantiated before memory can be allocated;
Static methods are more efficient than instantiation. The disadvantage of static methods is that they are not automatically destroyed, while instantiated ones can Destroy;
Static methods can be used before creating the object, and non-static methods must be called through the new object. .
Static methods can be called directly through class name::method name. Ordinary methods need to create an instance, that is, a new object, and then call it through the object name -> method name; static classes can only contain static members, otherwise a compilation error will be thrown;
Non-static classes can contain both non-static members and static members. Static classes cannot be instantiated. The reason why they cannot be instantiated is because static classes will cause the C# compiler to mark the class as abstract and sealed at the same time. And the compiler will not generate an instance constructor in the type, resulting in static classes that cannot be instantiated;
Non-static classes can, and static members can only be accessed through classes. access, because static members belong to the class.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between php static methods and ordinary methods. For more information, please follow other related articles on the PHP Chinese website!