In PHP, member methods modified by the static keyword are called static methods. The memory space of static methods is fixed. Only static members in the class can be accessed through "class name::static method()" statement to access static methods.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, pass the static key The member properties and member methods modified by the word are called static properties and static methods. Here they can be collectively referred to as static members. Static members in a class are different from general members in the class. Static members will not be instantiated to In the object, that is to say, we can access static members through the class without instantiating a class.
It is easy to declare static variables in a class. We can add a static keyword before an ordinary member of the class to turn this ordinary member into a static member. In this way, we can directly access these static members in the class without instantiating the class. The syntax format for accessing static members is as follows:
类名::$静态属性 类名::静态方法()
The ::
symbol is called the range resolution operator, which is used to access static members, static methods and constants, and can also be used to override classes members and methods in .
If you want to access static properties in a member method inside a class, just add the operator self::
before the name of the static property.
Static method
The memory space of static method 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.
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 values, which can be used by all objects of the class;
(3) No An instance object is required to access static properties or methods.
The difference between static methods and ordinary methods
Static methods can be used before creating an object, non-static methods The method must be called through the object produced by new.
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 the static class through the object name -> method name. It 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, causing static classes to not be instantiated;
Non-static classes can, and static members can only be accessed through the class Access, because static members belong to the class.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is a static method in php. For more information, please follow other related articles on the PHP Chinese website!