has been dependent on the static keyword in PHP is blurred, just a few times in a single-case mode. Surf the internet to check, did not find a very full introduction, to summarize their own.
According to the use of the location is divided into two parts 1, the function body static variable 2, the static properties and methods in the class
1 static variables in the function body
Static$a =1; function Num1 () {Static$a =0; return$a + +;} function num2 () {$a=5555; Global$a; Echo $a++;} function num3 () {Static$a = -; return$a + +;} Echo Num1 (); //0 num2 (); //1 echo $a; //2 echo num3 (); //100 echo num3 (); //101 echo num3 (); //102
What are some of the things we see
The static variables in the 1 function body do not conflict with the static variables in the global, only the local and global unification can be made under the keyword Global action.
2 static variables in the function body are initialized only once when the function is called
Because the static variable allocates memory in the global data area, it is to be mentioned that the cache is logically roughly differentiated from the stack heap global program code area literal constant area
Stack store variable name (there will be a part of the integer, floating point type, string stored here)
Heap Store variable values
Global zone holds static variables, global variables
The program code area holds the functions and methods to be executed
Literal constant area storage constants, etc.
It's roughly the way it is, quite not strict, but it can help us remember the relationship between the data.
Static members in the 2 class
Static members in a class contain static properties and static methods
class First { public $num = 1;
//declaring a static property Static $name= ' 456 '; //declaring a static method Public Static functionSelf_use () {echo self:: $name;
EchoSelf::out (); } Public Static functionStatic_use () {Echo Static::out (); } Public Static functionmethod () {Echo' First '; } //This is a bad call. Public functionMy_wrong () {//echo Self::d is (); } Public functiondis () {Echo' Just do a show '; }}classSecondextendsfirst{ Public Static functionmethod () {Echo' Second '; }}$one=NewFirst ();$one::$name;$one-Self_use ();
$one-Static_use ();
$two New second ();
$two-Self_use ();
$two->static_use ();
About static methods in classes we introduce from two angles 1 using angle 2 theoretical angle
1 in practical use we need to pay attention to several points
A. Static methods can call static properties and prohibit calls to non-static properties.
B. $this is not allowed within a static method, and the script is stopped when it appears.
C. Other static methods in the calling class in a static method have two formats, self: and static. Based on the output, we learned that the difference between the two methods is to use Self::method () to call the method () method of the class in which the methods are located. With Static::method (), the last method defined by the entire inheritance family is called.
This feeling is very round, the image point of understanding is to declare a static method A in the parent class, the subclass overrides the static method A. Calling a with self in other methods of the parent class executes the method of a in the parent class, but the method of subclass A is executed using the static call;
2 Introduction from a theoretical perspective
Early structured programming is almost always a static method, and after the object-oriented approach, there is an example of the method of instantiation. Object-oriented does not completely solve the problem of using static methods. We can understand that static methods and instantiation methods are all classes, but they are called differently, and static methods and properties are used in classes that use class:: They are stored in memory and then used. However, instantiating a method and a non-static property requires instantiating new before it is stored in memory. This explains that a static method can not invoke a non-static property, you call a voice static method out, I am not static properties Ah, not approved (new) I do not come out Ah, and B in the $this is an object-oriented product specifically used to instantiate the object when the object is called properties and methods used, is the exclusive skill of the object, the static method of this skill cannot be used.
Summing up here for the summary after reading, I have so high level of wood:
There is a common understanding of this problem: the instantiation method is more used and secure, and the static method is less used. Sometimes we have some misunderstanding about static methods and instantiation methods.
1, we all think " static method resident memory, the instance method is not, so the static method is high efficiency but occupies memory." "
In fact, they are all the same, when loading the timing and taking up memory, the static method and the instance method are the same, loaded when the type is used for the first time. The speed of the call is basically no different.
2. Everyone thought " static method allocates memory on heap, instance method on stack "
In fact, none of the methods can allocate memory on the heap or on the stack, as the code is loaded into a special area of code memory that is not writable.
Method takes up no more memory, and it doesn't matter whether it's static or not.
Because fields are used to store information for each instance object, the fields occupy memory, and because each instance object's state is inconsistent (at least not considered consistent), all the fields of each instance object have a copy of the memory. And that's why you can use them to tell which object you're working on.
But the method is different, no matter how many instance objects, the code of the method is the same, so as long as there is a copy of the code is enough. So either the static or the Non-static method, there is only one copy of the code, that is, to occupy only a portion of memory space.
The same code, why does it behave differently? This relies on the data used by the method. There are two main sources of data, one is through the parameters of the method, and the other is to use the value of the class member variable ...
3, we all think that " instance method needs to create an instance before it can be called, more trouble, static method is not, relatively simple "
In fact, if a method has nothing to do with the instance object of his class, it should be static and should not be written as an instance method. So all the example methods are related to the instance, since it is related to the instance, then creating an instance is the inevitable step, without the hassle of simply saying it.
Of course, you can completely write all the instance methods as static, passing the instance as a parameter, in general there may not be any problem.
From an object-oriented perspective, when you choose to use an instantiated or static method, you should use a static method if you want to use an instantiated object, depending on whether the method and the instantiated object have a logical dependency. This is only from an object-oriented perspective.
If it is from the thread safety, performance, compatibility is also the choice of instantiation method is advisable.
Why do we divide the method area into: Static and instantiation methods?
If we continue to study in depth, we must leave the technology to talk about theory. Early structured programming, almost all methods are "static methods", the introduction of the concept of instantiation is the object-oriented concept after the emergence of things, the distinction between static and instantiation methods can not only from the performance to understand, create c++,java,c# In this way, the introduction of the object-oriented language of the master is not to solve the problem of performance, memory, but in order to make development more modular, object-oriented. In this case, the static method and the instantiation mode are differentiated to solve the problem of the pattern.
Take someone else an example to say things:
For example, "people" This class, everyone has a name, age, gender, height, etc., these attributes should be non-static, because each of these attributes are not the same, but the biology of which the door which the goal of which, this attribute belongs to the whole human, so it should be static-- It does not depend on a particular person, and there is no one who is a "vertebrate mammal" and some one is "Artiodactyla".
Reference documents:
Static keyword and variable storage location summary
Static statically-keyword in PHP