Understand the Static and Const keywords in PHP5. PHP5 has added a lot of object-oriented ideas. PHP5's object-oriented thinking is closer to Java's object-oriented thinking. Here we will describe the role of static and const in PHP5. I hope PHP5 has added a lot of object-oriented ideas. PHP5's object-oriented thinking is closer to Java's object-oriented thinking. Here we will describe the role of static and const in PHP5, hoping to help those who are learning PHP5.
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.
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 will occur.
Example: (Note: The following code is from phpe.net)
Private static $ count = 0; // defines a static attribute
Const VERSION = 2.0; // define a constant
Function _ construct ()
Self: $ count;
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 ()."
N "); // use the direct input class name to access the static method Counter: getCount
// Print the version of the class
Print ("Version useed:". Counter: VERSION ."
Bytes. Here we will describe the role of static and const in PHP5...