Protected and const properties of PHP classes and objects _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.