Stephanie Jacobsen Understanding the difference between static and const keywords in PHP5

Source: Internet
Author: User
PHP5 has added a lot of object-oriented ideas, and PHP5 object-oriented approach to the object-oriented concept of java. Here we describe the role of static and const keywords in PHP5, hoping to help friends who learn PHP5.
(1) static
The static keyword is in the class, which describes a member that is static, and static can restrict external access, because the static member belongs to the class, does not belong to any object instance, the other class is inaccessible, only the instance of the class is shared, and the member is protected by a certain program. 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.
(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
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)

Copy the Code code as follows:


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 ");
?>


Well, basic to here, the heart know that something to speak clearly, but I feel that static still a bit do not understand, please expert guidance!

The above describes the Stephanie Jacobsen understand the difference between the static and const keywords in PHP5, including the Stephanie Jacobsen aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.