Understand the difference between static and const keywords in PHP5_PHP Tutorial

WBOY
Release: 2016-07-21 15:56:25
Original
751 people have browsed it

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 is used to describe a member as static. static can restrict external access, because the members after static belong to the class and do not belong to any object instance. Others The class is inaccessible and is only shared by instances of the class. A certain program must protect 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 defining a constant, 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 direct input Class name to access the static method Counter::getCount
//Print the version of the class
print( "Version used: " .Counter::VERSION. "
n" );
?>

Well, basically at this point, I have explained clearly what I know in my heart, but I feel that I still don’t understand static a little, please give me some advice!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318067.htmlTechArticlePHP5 has added a lot of object-oriented ideas. 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. I hope...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template