protected and const attribute in PHP class

Source: Internet
Author: User
Tags constant php class

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);
?>

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.