Const property
The field defined with the Const property is a constant, and the constants in the class are similar to static variables, but the difference is that once the value of a constant is assigned, it cannot be changed. The const definition constant does not need to be added with the $ symbol, which is structured as follows:
const constant name//constant name cannot be signed with $
1. Constant attributes are declared with the Const keyword, unlike regular attributes, which begin with the dollar sign $;
2, according to the Convention, can only use the capital letter to name the constant;
3. Like global variables, class constants cannot be changed once they are set;
4, contains only the basic data type value, cannot assign an object to the constant;
5. Like static properties, constants can only be accessed through classes and not through instances of classes (objects);
6, reference constants do not need to use the dollar sign as a leader;
7. Assigning values to constants that have been declared can cause parsing errors;
8. You should use constants when you need to have access to a property in all the examples of a class, and the property value does not need to be changed.
The code is as follows |
Copy Code |
<?php Header (' Content-type:text/html;charset=utf-8 '); Class shopproduct{ Const Guowanpiaopen = "Guo Bowl scoop basin"; Const BLOGTITLE = "The beginning of a good life!" "; //... Public Function SayHello () { Print Shopproduct::guowanpiaopen. " -". Shopproduct::blogtitle. " <br/> "; Note that each reference constant must point to the current class (the current class name plus two colons) Print Self::guowanpiaopen. " -". Self::blogtitle." <br/> "; Here the Self keyword points to the current class, acting as above } }
Print Shopproduct::sayhello (); Print Shopproduct::guowanpiaopen; ?> |
Protected property
Protected a qualified field is between public and private, and if the member is declared protected (protected), the field can only be used in the class and subclasses of that class.
The instance code is as follows:
The code is as follows |
Copy Code |
Class me{ protected $Money = 100; protected $price 1=60;
Public Function Sell ($price) { if ($this->price1<= $price) { echo "Well, sell it to you." "; $this->money = $this->money+ $price; Return "I now have a total". $this->money. "Yuan money"; } else{ echo "I don't sell, $price's too cheap. "; Return "Now I still". $this->money. "Yuan money"; } } } $now =new me; Echo $now->sell (30); ?> |