This article introduces you to the protected and const attribute usages of classes and objects in PHP, as well as references for friends who need to know.
Const property
The field defined with the const attribute is a constant, and the constant in the class is similar to a static variable, where the value of the constant cannot be changed once it is assigned.
The const definition constant does not require a $ symbol and is structured as follows:
const constant name//constant name cannot be used with $ symbol
Instance:
The code is as follows |
Copy Code |
Class date{ Const m= "Monday"; } echo "Today is". date::m; ?> |
Tip: constant names defined with const are generally capitalized, which is a convention that we want to develop into a good naming habit. If a defined constant consists of multiple words, the underscore link is used, which is also a convention. For example: File_size.
Protected property
Protected scoped field scope between public and private, if the member is declared protected (protected), the field is used only in the class and in the subclass of the class.
Instance:
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 "Okay, I'll sell it to you." "; $this->money = $this->money+ $price; Return "I have in total now". $this->money. "Yuan Qian"; } else{ echo "I don't sell, $price's too cheap. "; Return "Now I am". $this->money. "Yuan Qian"; } } }
$now =new me; Echo $now->sell (30); ?> |
http://www.bkjia.com/PHPjc/628819.html www.bkjia.com true http://www.bkjia.com/PHPjc/628819.html techarticle This article introduces you to the protected and const attribute usages of classes and objects in PHP, as well as references for friends who need to know. The Const property is defined with the Const property of a field that is a constant, class ...