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 is in a class, describing a member as static. Static can restrict external access, because members after static belong to the class and do not belong to any object instance, and are inaccessible to other classes. Yes, it is only shared with instances of the class, ensuring 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 constants , similar to #define in C, can define a constant. If its value is changed in the program, an error will occur.
Example the above code: (Note: The following code is from phpe.net)
Copy the code The code is as follows:
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(). "
n" ); //Use the direct input class name to access the static method Counter::getCount
//Print the version of the class
print( "Version used: " .Counter::VERSION. "
n" );
?>
The above introduces stephanie jacobsen's understanding of the difference between static and const keywords in PHP5, including stephanie jacobsen's content. I hope it will be helpful to friends who are interested in PHP tutorials.