Home > Backend Development > PHP Problem > What is the difference between php static methods and ordinary methods

What is the difference between php static methods and ordinary methods

青灯夜游
Release: 2023-03-08 16:40:01
Original
3115 people have browsed it

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.

What is the difference between php static methods and ordinary methods

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Static method

  • 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.

Ordinary methods

  • 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;

The main difference

  • 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template