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