A lot of object-oriented ideas have been added to PHP5. The object-oriented ideas of PHP5 are closer to the object-oriented ideas of Java. Here we will describe the functions of static and const keywords in PHP5, hoping to be helpful to friends who are learning PHP5.
(1) static
The static keyword in a class describes that a member is static. Static can restrict external access, because the members after static belong to the class and do not belong to any object instance. They are inaccessible to other classes and are only accessible to the class. Instance sharing can ensure that the program fully protects the members. Static variables of a class are very similar to global variables and can be shared by all instances of the class. The same is true for static methods of a class, similar to global functions. Static methods of a class can access static properties of the class. In addition, static members must be accessed using self. 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
const is a keyword that defines a constant, similar to #define in C. It can define a constant. If its value is changed in the program, an error will occur.
Give an example of the above code: (Note: The following code comes from phpe.net)
<?php
class Counter
{
private static $count = 0;//Define a static property
const VERSION = 2.0;//Define a constant
//Constructor
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();
//Perform printing
print( Counter::getCount(). "<br>n" ); //Use direct input of the class name to access the static method Counter::getCount
//Print the version of the class
print( "Version used: " .Counter::VERSION. "<br>n" );
?>
Well, basically at this point, I have made it clear what I know in my heart, but I feel that I am still a little uncomfortable with static. I understand, please give me some guidance!
My email: [email protected]
WriteTime 2004-11-3 21:00
http://dev.csdn.net/author/heiyeshuwu/23f0d08c11bc4509b41844855d5c7063.html