This article introduces the use of protected and const attributes of classes and objects in php. For more information, see. the field defined by the const attribute in the const attribute is a constant. constants in the class are similar to static variables. The difference is that... this article introduces the use of protected and const attributes of classes and objects in php. For more information, see.
Const attributes
The field defined with the const attribute is a constant. constants in the class are similar to static variables. The difference is that the value of a constant cannot be changed once it is assigned a value. the const definition constant does not require the $ symbol. its structure is as follows:
Const constant name // constant name cannot use the $ symbol
The instance code is as follows:
Tip: The constant names defined by const are generally capitalized. this is a convention. we need to develop a good naming convention. if the defined constant is composed of multiple words, the underscore _ link is used, which is also a convention. example: FILE_SIZE.
Protected attributes
The field scope defined by protected is between public and private. if this member is declared as protected (protected), it means that this field can only be used in this class and its subclass.
The instance code is as follows:
Price1 <= $ price) {echo "good, sold to you. "; $ this-> Money = $ this-> Money + $ price; return" I have a total ". $ this-> Money. "Yuan money";} else {echo "I don't sell, $ price is too cheap"; return "I am still ". $ this-> Money. "RMB" ;}}$ now = new me; echo $ now-> RMB (30);?>
Address:
Reprinted at will, but please attach the article address :-)