Introduction to the use of static variable statics in PHP _php tutorial

Source: Internet
Author: User
The static keyword is very common in C # programming, and it is used to declare static members that belong to the type itself rather than to a particular object. The static modifier can be used for classes, fields, methods, properties, operators, events, and constructors, but not for types other than indexers, destructors, or classes. Classes, functions, and variables declared as static will not be able to reference instance methods or variables, and in C # Once the class has been added with the static modifier, all of its internal variables and methods must be static. Static variables and methods must be referenced through the class name and not through the instance object.

So what's the difference between the static keyword and C # in PHP?

Declaration scope

Compared to C #, the use of static variables in PHP is more extensive, we can not only add static modifiers to the class, method, or variable, we can even add the static keyword to the function's internal variables. A variable with the static modifier added is not lost even after the function has finished executing, that is, the variable remembers the original value the next time the function is called. Such as:

 
  

The results of the operation are as follows:

3 5 7

One thing to note here is that the assignment of a variable will only be called when the variable is first initialized, and will not be invoked during the execution of the function.

Since functions in PHP are also class-one citizens, unlike C #, we can directly define functions and invoke them directly anywhere in the code, which is somewhat similar to JavaScript. So this time the function static variable is more useful than defining a global variable, which avoids duplicate definitions of variables that cause conflicts. Because functions in C # cannot be directly defined and called, it must be hosted in a class, so if a function requires a static variable, we only need to define it in the class to achieve the same effect.

Invocation mode

In C #, we call static members in a very simple and consistent way, because static members are not part of an instance object, so either a method or a variable, C # accesses its static members through the class name. Method (variable). And in C #, static functions cannot be set to virtual methods or overwritten. PHP provides a more flexible and versatile support for this.

First of all, we know that invoking instance methods in PHP is called by Someobj->somefun (), so can we call a static function to call it through Someclass->somefun () like C #? The answer is no, in PHP, the call to a static member can only be done by:: Someclass::somefun ().

 
  Var2. ' ';    }    public static function T2 () {self        :: $var 1+=2;        echo Self:: $var 1. ' ';    }} $t =new TESTC (); $t->t1 (); Testc::t2 ();?>

The results of the operation are as follows:

3 1 5

Another point that differs from C # is that in a method in a class, if we need to invoke a static variable, we must pass self: $somVar static variable (note that the $ symbol before the variable, the instance variable is not required), and the static method is called Self::somefun () (This does not require the $ symbol). As in the above example.

In addition, the biggest difference with C # is that in PHP, subclasses can override the parent class's static function or variable, not only that, (from the perspective of the C # Programmer, I think the PHP point is complicated), because the default Self::staticfun () call is a subclass of the static function , what if we want to invoke the static variables of the parent class? Here PHP provides an additional parent to invoke the static members of the base class. Such as:

 
  

The results of the operation are as follows:

3 5 ' Hello '

Best of all, according to the above example, it is easy to think that the subclass access parent class can use the parent keyword, then how does the parent class access the static method of the subclass? Here's another way to use static, where if you change the scope of the preceding static method to static, PHP calculates the final static method based on the class's inheritance hierarchy. Such as:

 
  

The results of the operation are as follows:

Test2 Test2

Here the T instance finds the final static method and outputs Test2 based on its instance when the T1 method invokes the T2 static method.

Summarize

From the above analysis, it is not difficult to see that, for the use of static members, PHP provides more powerful than C # functionality or flexibility, but from my point of view, this flexibility is not necessarily better, from a certain point of view, if the class inheritance hierarchy is too complex, it may confuse me. Of course, the same tool will work differently for different people, since PHP offers a more diverse selection, then believe that if used properly, the static in PHP may provide a more powerful and easy way to use than in C #.

http://www.bkjia.com/PHPjc/752477.html www.bkjia.com true http://www.bkjia.com/PHPjc/752477.html techarticle The static keyword is very common in C # programming, and it is used to declare static members that belong to the type itself rather than to a particular object. The static modifier can be used for classes, fields, methods, genera ...

  • 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.