{PHP beginners: static const global}

Source: Internet
Author: User

(1) static
The static keyword is in the class, describing a member is static, and static can restrict external access, because the static member belongs to the class and does not belong to any object instance, other classes cannot be accessed. They are shared only to instances of the class.ProgramDo your best to protect this member. Static variables of the class are very similar to global variables and can be shared by all class instances. Static Methods of the class are also the same, similar to global functions. The static method of the class can be used to initialize the static attributes of the class. In addition, static members must use self for access. If this is used, an error occurs.
(For the similarities and differences between this and self, see: http://blog.csdn.net/heiyeshuwu/archive/2004/11/03/165828.aspx)

(2) const
Const is a key word that defines constants. Similar to # define in C, const can define a constant. If its value is changed in the program, an error will occur.

 <?  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 -- ;
}

// Define a static method
Static Function getcount ()
{
Return SELF: $ count;
}
}

// Create an instance
$ C = New Counter ();

// Print
Print (counter: getcount (). " <Br> \ n " ); // Directly enter a class name to access the static method counter: getcount

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

?>

Well, the things I know in my heart are clearly explained here, but I don't think I understand static. Please give me some advice!

Static and global are both global variables. Static is called static global variables and Global is called static global variables.
The global variables in PHP are a little different from those in C language. In C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. Some may carelessly change a global variable. Global variables in PHP must be declared as global when used in functions.
Example of using global

 <?  PHP
Function isnotglobal (){
Echo " <Br> isnotglobal: A = $ " ;
}
Function isglobal (){
Global $; // $ A only acts as a global variable in the entire function. It can call $ A of the external main function, but cannot call $ A of other external functions.
Echo " <Br> isglobal: A = $ " ;
}
$ = 1 ;
Isnotglobal ();
Isglobal ();
?>

Output result: isnotglobal: A =
Isglobal: a = 1
The global variable $ A can be called here. If you change global to static,
Output result: isnotglobal: A = isglobal: A = Why? Because the static function can only act as a global variable in the isglobal () function, this function does not work.
See the following section.Code:

 <? PHP
Function isnotstatic (){
$ X + = 1 ;
Echo " <Br> X = $ x " ;
}
Function isstatic (){
Static $ X; // $ X only acts as a global variable in the isstatic function.
$ X + = 1 ;
Echo " <Br> X = $ x " ;
}
Isnotstatic ();
Isnotstatic ();
Isstatic ();
Isstatic ();
?>

Output result x = 1
X = 1
X = 1
X = 2
All referenced variables of any variable point to global variables. Static is a class variable. Its value cannot be changed as web_ajax says, so that it can be changed.

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.