Php targets the call of static attributes and static methods. Php object-oriented call of static attributes and static methods this article mainly introduces the call of static attributes and static methods in php object-oriented, the instance analyzes static attributes and calls of static methods in php object-oriented static attributes.
This article mainly introduces the call of static attributes and static methods in php object orientation. The example analyzes the principles and call skills of static attributes and static methods. For more information, see
This document describes how to call static attributes and methods in php. Share it with you for your reference. The details are as follows:
The static attributes and call of static methods in php object-oriented are analyzed here. Regarding their calls (whether they can be called or how to call them), you need to understand their location in the memory, which is very easy to understand. Static attributes and methods (including static and non-static attributes) have only one location in the memory (instead of static attributes, the attributes of how many instantiated objects are available ).
Instance:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Header ("content-type: text/html; charset = utf-8 "); Class Human { Static public $ name = "Xiaomei "; Public $ height = 180; Static public function tell (){ Echo self: $ name; // static method call static attribute, using the self keyword // Echo $ this-> height; // error. Static methods cannot call non-static attributes. // Because $ this represents the instantiated object, and this is a class, I don't know which object $ this represents } Public function say (){ Echo self: $ name. "I spoke "; // Call static attributes using common methods. the self keyword is also used. Echo $ this-> height; } } $ P1 = new Human (); $ P1-> say (); $ P1-> tell (); // object can access static methods Echo $ p1: $ name; // static attribute of object access. $ P1-> name cannot be accessed like this // Because the memory location of the static attribute is not in the object Human: say (); // error. An error occurs when the say () method $ this exists; when $ this exists, the result is returned. // If php5.4 or above is used, a prompt is displayed. ?> |
Conclusion:
(1) Static attributes can be called without instantiation. Because static attributes are stored in the class, the calling method is "class name: Property Name ";
(2) static methods can be called without instantiation. Same as above
(3) static methods cannot call non-static attributes. Because non-static attributes need to be instantiated and stored in objects;
(4) static methods can call non-static methods and use the self keyword. In php, after a method is self:, it is automatically converted to a static method;
I hope this article will help you with php programming.
This article describes the call of static attributes and static methods in php object orientation. the instance analyzes static attributes...