PHP5 has added a lot of object-oriented ideas, and PHP5 object-oriented approach to the object-oriented concept of java. Here we describe the role of static and const keywords in PHP5, hoping to help friends who learn PHP5.
(1) static
The static keyword is in the class, which describes a member that is static, and static can restrict external access, because the static member belongs to the class, does not belong to any object instance, the other class is inaccessible, only the instance of the class is shared, and the member is protected by a certain program. The static variables of a class, very similar to global variables, can be shared by instances of all classes, and the static methods of classes are the same, similar to global functions. The static method of the class can access the static properties of the class. It is also stated that the static member must be accessed using self, and using this will cause an error.
(for the similarities and differences between this and self, please refer to: http://blog.csdn.net/heiyeshuwu/archive/2004/11/03/165828.aspx)
(2) const
A const is a keyword that defines a constant, similar to a # define in C, that defines a constant, and an error occurs if its value is changed in the program.
Illustrate the above code: (Note: The following code is from Phpe.net)
Copy the Code code as follows:
Class Counter
{
private static $count = 0;//defines a static property
Const VERSION = 2.0;//defines a constant
constructor function
function __construct ()
{
Self:: $count + +;
}
Destructors
function __destruct ()
{
Self:: $count--;
}
To define a static method
static function GetCount ()
{
Return self:: $count;
}
}
Create an instance
$c = new Counter ();
Perform printing
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. "
n ");
?>
Well, basic to here, the heart know that something to speak clearly, but I feel that static still a bit do not understand, please expert guidance!
The above describes the Stephanie Jacobsen understand the difference between the static and const keywords in PHP5, including the Stephanie Jacobsen aspects of the content, I hope that the PHP tutorial interested in a friend helpful.