Understand the Static and Const keywords in PHP5

xiaoxiao2021-03-06  87

Understand the Static and Const keywords in PHP5

Text / Heiyeluren2004 / 11/3

A lot of object-oriented ideas are added to PHP5, and the object-oriented object-oriented objects of PHP5 is close to Java. We will describe the Static and Const keywords in PHP5 here, I hope to help with friends who learn PHP5.

(1) StaticStatic keyword is in the class, describing a member is static, static can limit external access, because STATIC's member belongs to the class, is not any object instance, other classes are unable to access, only Sharing the instance of the class, can be protected against the member. The static variable of the class is very similar to global variables, which can be shared by all classes, and the static method of the class is also the same, similar to the global function. The static method of the class can access the static properties of the class. Alternatively, STATIC's member must be accessed using Self to use this. (For THIS and SELF, please refer to: http://blog.9cbs.net/heiyeshuwu/archive/2004/11/03/165828.aspx)

(2) ConstConst is a keyword that defines a constant, similar to #define in C, can define a constant, if it changes its value in the program, there will be an error.

For example, the above code: (Note: The following code is from phpe.net)

Class counter {private static $ count = 0; // Define a static attribute const version = 2.0; // Define a constant

// Constructor function __construct () {self: $ count ;} // destructive function function __destruct () {self: $ count -;} // Define a static method static function getCount () {Return Self :: $ count;}}

// Create an instance $ c = new counter ();

// Perform a print print (counter :: getCount (). "
/ n"); // Use the direct input class name to access the static method Counter :: getCount

// Print the version of the version Print ("Version Useed:" .counter :: Version. "
/ n");

?>

Well, basically here, the thing you know is clear, but I feel that I have a little dishever for Static, please expert guidance! My email: heiyeluren@163.com

Writetime 2004-11-3 21:00

转载请注明原文地址:https://www.9cbs.com/read-96689.html

New Post(0)