First, static variables
1. A static variable is a variable that exists only in scope, but the value of the variable is not lost after the function is executed, that is, the variable will still remember the original value the next time the function is called.
2. It is illegal to pay a static variable the result of an expression calculation. An expression is any statement that the value of itself changes, for example, $variable, Anyfunc () is an expression.
3. You might think that defining static variables does not seem to be very useful because you can easily get the same results with global variables. However, global variables can be accessed by all functions, so if two or more of the functions that are supposed to be independent are using variables of the same name, it will result in conflicts, and there is no need for more syntax to use static variables than to introduce global variables. Therefore, when only one function needs to access a variable, we should take precedence over the static variable instead of the global variable.
Second, the use of static elements in the class
4. In a class, the static keyword has two main uses, one for defining static members and the other for defining static methods.
5. A static member is a class variable that can be thought of as belonging to an entire class and not to an instance of a class. Unlike a typical instance variable, a static member retains only one variable value, and the value of that variable is valid for all instances, that is, all instances share this member.
6. $this represents only an instance of the current class, Self:: Represents the class itself (plus $ later)
7. Scope-Qualified operators::
With scope-scoped operators, we can use two system-defined scopes: Self and parent. In addition, the php5.3 provides a static scope. Self represents the scope of the current class, but unlike $this, it does not represent a particular instance of the class. This operator cannot be used in code other than the class, and it does not recognize its position in the inheritance tree hierarchy. That is, when you use the self scope in an extension class, self can call the method declared in the base class, but it always calls methods that have been overridden in the extension class. (equivalent to overwriting of methods in Java, explicitly invoking the parent class method with parent)
Overriding a static variable of a parent class defines the same static variable in the child class
PHP code
<span style= "Font-size:small;" ><?php
Class AAA
{
function bbb () {
echo ' Double colon:: Method of calling Class! ';
}
}
Call 1:
$a = new AAA ();
$a->bbb ();
Call 2:
AAA::BBB ();
?> </span>
The two methods get the same result.
Do not understand why the ordinary method BBB can also be used directly with the class name:: Call, which seems to be different from the inside of Java
Java code
<span style= "Font-size:small;" >test.java
Class test{
Common methods
public void Eat () {
System.out.println ("no-static!");
}
Static methods
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 certainly be accessed, but the eclipse and other editors will suggest calling the class name directly
(The static method show () from the type Test should is accessed in a static)
Test.show ();//Output static
Test.eat ();//error cannot refer to a non-static method from a static context eat () PHP can endure ...
}
}</span>
Personal understanding: As long as the called Ordinary method does not appear $this->xx ($this only the instance of the current class), the common method can also be considered as directly belong to the class, so you can directly call with the class name, Unlike Java, methods must be declared static in order to be called directly with the class name (PHP6 has made several modifications to the operation of the static method, first when accessing a non-static method, no longer using:: Operator!). This way is going to be killed ... )
Methods that help the parent class call the child class
Static in php5.3: scope 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 calculates the members of the last class on the inheritance hierarchy immediately before the code executes. This process is known as delayed binding. It seems that there is no use, directly in the parent class new that the final implementation of the object of the class and then call it OK? But if you don't know what the final class is called and what effect the overridden method achieves, it seems to be useful at this time. Static:: Scopes can also be applied on static methods, so that methods of subclasses can be called statically from the parent class.