PHP5 has added a lot of object-oriented ideas, and PHP5 object-oriented approach to the object-oriented concept of java. We here to PHP5 in the static and const key word role to describe, hope to learn PHP5 friends help.
(1) static
Static keywords in the class is, describes a member is static, static can restrict the external access, because the static member is a class, is not part of any object instance, the other classes are inaccessible, only the instance of the class is shared, can be a certain program to protect the member. 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.
(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)
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 ");
?>
http://www.bkjia.com/PHPjc/445552.html www.bkjia.com true http://www.bkjia.com/PHPjc/445552.html techarticle 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 static and const key characters in PHP5, and hope ...