Static variables in PHP5.3

Source: Internet
Author: User
I. static variables 1. static variables only exist in the scope of the variable. However, after the function is executed, the value of such variables will not be lost. that is to say, when the function is called the next time, the variable still exists... i. static variables
1. static variables only exist in the scope. However, after the function is executed, the value of such variables will not be lost. that is to say, when the function is called the next time, the variable still remembers the original value.
2. it is illegal to pay the result calculated by the expression to the static variable. An expression is any statement that changes its own value. for example, (1 + 1), $ variable, and anyfunc () are expressions.
3. you may think that defining static variables does not seem very useful, because global variables can be used to easily obtain the same results. However, global variables can be accessed by all functions. Therefore, if two or more independent functions use variables with the same name, a conflict may occur, compared to introducing global variables, static variables do not require more syntax. Therefore, when only one function needs to access a variable, we should first use static variables instead of global variables.

II. use of static elements in the class
4. in a class, the static keyword has two main usage methods: one is to define static members and the other is to define static methods.
5. a static member is a type variable that can be viewed as a whole class rather than an instance of the class. Unlike general instance variables, static members only retain one variable value, which is valid for all instances, that is, all instances share this member.
6. $ this indicates only the instance of the current class, and self: indicates the class itself (followed by $)
7. limited scope operator ::
Through the scope limitation operator, we can use two system-defined scopes: self and parent. Php5.3 also provides static scopes. Self indicates the scope of the current class, but unlike $ this, it does not indicate a specific instance of the class. This operator cannot be used in code outside the class, and it cannot recognize its position in the hierarchy of the inheritance tree. That is to say, when the self scope is used in the extension class, self can call the methods declared in the base class, but it always calls methods that have been rewritten in the extension class. (This is equivalent to overwriting methods in java, and parent is used to call methods of the parent class explicitly)

Override the static variables of the parent class and define the same static variables in the subclass.

Php code

Class aaa

{

Function bbb (){

Echo 'use double colon: To call the class method! ';

}

}

// Call 1:

$ A = new aaa ();

$ A-> bbb ();

// Call 2:

Aaa: bbb ();

?>

The results of the two methods are the same.

I cannot understand why the common method bbb can also be called directly with the class name:, which seems to be different from that in java.



Java code

Test. java

Class Test {

// Common method

Public void eat (){

System. out. println ("no-static! ");

}

// Static method

Public static void show (){

System. out. println ("static! ");

}

// Main method

Public static void main (String args []) {

Test obj = new Test ();

Obj. eat (); // output no-static!

Obj. show (); // The method belongs to the class, and the instance of the class can also be accessed. However, a yellow prompt is displayed in the eclipse editor and it is recommended that you use the class name to directly call the method.

// (The static method show () from the type Test shocould be accessed in a static way)

Test. show (); // output static

// Test. eat (); // The error message cannot reference the non-static method eat () php from the static context, but it is tolerable...

}

}

Personal Understanding: as long as $ this-> xx ($ this only indicates the instance of the current class) is not displayed in the called common method, the common method can also be considered as directly a class, therefore, you can directly use the class name, instead of using the class name to directly call a method that must be declared as static in java (php6 has made several modifications to the operation of the static method, first, when accessing non-static methods, you no longer use the: operator! This method should be killed ...)


How to help the parent class call subclass
The static: scope mentioned in php5.3 makes it unnecessary for us to use self: And parent ::. When you want to point to the final implementation class, you can use static:. this qualifier will calculate the last class member in the inheritance hierarchy before the code is executed. This process is called delayed binding. It seems that there is nothing to do with it. simply add the final implementation class object in the parent class and then call it. isn't that enough? However, if you don't know what the final class is called and what effect the overwriting method achieves, it seems useful at this time. Static: The scope can also be applied to static methods, so that sub-class methods can be statically called from the parent class.

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.