Usage of static and const keywords in php5

Source: Internet
Author: User
In php5, the usage of static and const keywords has added a lot of object-oriented ideas to php5. php5's object-oriented thinking is similar to Java's object-oriented thinking. In php5, the usage of static and const keywords adds a lot of object-oriented ideas to php5. php5's object-oriented thinking is closer to Java's object-oriented thinking. Here we will describe the role of the static and const keywords in php5, and hope it will be helpful for friends who want to learn php5.
(1) static
The static keyword is in the class, describing a member is static, and static can restrict external access, because the static member belongs to the class and does not belong to any object instance, other classes cannot be accessed. they are shared only to the instance of the class and can be fully protected by the program. Static variables of the class are very similar to global variables and can be shared by all class instances. static methods of the class are also the same, similar to global functions. The static method of the class can be used to initialize the static attributes of the class. In addition, static members must use self for access. if this is used, an error occurs. (This, self usage see http://www.phpzixue.cn/detail498.shtml)
(2) const
Const is a key word that defines constants. similar to # define in C, const can define a constant. if its value is changed in the program, an error will occur.
The above code is illustrated as an example:
   Class Counter
{
Private static $ count = 0; // defines a static attribute
Const VERSION = 2.0; // define a constant
// Constructor
Function _ construct (){
Self: $ count ++;
}
// Destructor
Function _ destruct (){
Self: $ count --;
}
// Define a static method
Static function getCount (){
Return self: $ count;
}
}
// Create an instance
$ C = new Counter ();
// Execute print
Print (Counter: getCount ()."
"); // Use the directly input class name to access the static method Counter: getCount
// Print the version of the class
Print ("Version useed:". Counter: VERSION ."
");
?> In php5, the usage of static and const keywords adds a lot of object-oriented ideas to php5. php5's object-oriented thinking is closer to Java's object-oriented thinking. Here we will describe the role of the static and const keywords in php5, and hope it will be helpful for friends who want to learn php5.
(1) static
The static keyword is in the class, describing a member is static, and static can restrict external access, because the static member belongs to the class and does not belong to any object instance, other classes cannot be accessed. they are shared only to the instance of the class and can be fully protected by the program. Static variables of the class are very similar to global variables and can be shared by all class instances. static methods of the class are also the same, similar to global functions. The static method of the class can be used to initialize the static attributes of the class. In addition, static members must use self for access. if this is used, an error occurs. (This, self usage see http://www.phpzixue.cn/detail498.shtml)
(2) const
Const is a key word that defines constants. similar to # define in C, const can define a constant. if its value is changed in the program, an error will occur.
The above code is illustrated as an example:
   Class Counter
{
Private static $ count = 0; // defines a static attribute
Const VERSION = 2.0; // define a constant
// Constructor
Function _ construct (){
Self: $ count ++;
}
// Destructor
Function _ destruct (){
Self: $ count --;
}
// Define a static method
Static function getCount (){
Return self: $ count;
}
}
// Create an instance
$ C = new Counter ();
// Execute print
Print (Counter: getCount ()."
"); // Use the directly input class name to access the static method Counter: getCount
// Print the version of the class
Print ("Version useed:". Counter: VERSION ."
");
?>

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.