Static and Const keywords in PHP5

Source: Internet
Author: User

(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.
Using:: Calling a non-static method will result in an e_strict level 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 ");

Reference http://www.php100.com/html/php/hanshu/2013/0905/4424.html

Static and Const keywords in PHP5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.