Understanding the static and const keywords in PHP5

Source: Internet
Author: User
Tags constant count
php5| keyword

Many object-oriented ideas have been added to the PHP5, and the object-oriented approach of PHP5 is close to the object-oriented idea of Java. Here we have a description of the static and const keyword functions in PHP5, and hope to be of help to learn PHP5 's friends.

(1) static
The static keyword is in the class, describes a member is static, static can restrict access to external, because the static members belong to the class, is not part of any object instances, other classes are inaccessible, only to instances of the class to share, can be a certain program to protect the members. A static variable of a class, very similar to a global variable, can be shared by instances of all classes, and the static method of the class is the same, similar to the global function. Static methods of a class can access the static properties of a class. Additionally, static members must be accessed using self, and using this will make 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 a constant, similar to the #define in C, can define a constant, and if it changes its value in the program, an error occurs. The


provides an example of the above code: (Note: The following code comes from Phpe.net)

<?php

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-;
&NBSP}
 
 //Define a static method
 static function GetCount ()
 {
  return self::$ Count
 }
}

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

//Execute print
(counter::getcount) print (). "<br>\n"); Use the direct input class name to access the static method Counter::getcount

///print version of the class
print ("Version useed:". Counter::version. "<br>\n");

?

Well, basically here, the heart know that something will be clear, but I feel that I still have a little understanding of static, please expert advice!

My mailbox: heiyeluren@163.com

Writetime 2004-11-3 21:00



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.