Understand the static and const keywords in PHP5

Source: Internet
Author: User
PHP5 has participated in many object-oriented ideas. PHP5's object-oriented ideas are similar to Java's object-oriented ideas. Here we will describe the role of static and const in PHP5. I hope it will help my friends who are learning PHP5. (1) staticstatic

PHP5 has participated in many object-oriented ideas. PHP5's object-oriented ideas are similar to Java's object-oriented ideas. Here we will describe the role of static and const in PHP5. I hope it will help my friends who are learning PHP5.

(1) static

The static keyword is in the class that describes a member as static. static can restrict external visits. because the static member belongs to the class, it does not belong to any object instance, other classes cannot be visited. they are shared only to the instance of the class, and the program will be fully protected by this member. 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 visit the static attributes of the class. In addition, it is clarified that static members must use self to visit and this will cause errors.

(2) const

Const is the key word for defining constants. similar to # define in C, const can define a constant. if its value is changed in the program, an error is displayed.

The above code is illustrated as an example: (Note: The following code comes from phpe.net)

Class Counter
{
Private static $ count = 0; // defines a static attribute
Const VERSION = 2.0; // define a constant
// Structure function
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 ();
// Print fulfillment
Print (Counter: getCount ().'
\ N'); // the application directly enters the class name to visit the static method Counter: getCount
// Print the version of the class
Print ('version useed: '. Counter: Version .'
\ N ');
?>

Well, I can understand what I know in my heart, but I feel that I still don't understand static!

My mailbox: heiyeluren@163.com WriteTime 2004-11-3

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.