Introduction to static PHP static variables

Source: Internet
Author: User

Static keywords are very common in C # programming. They are used to modifier declarations that belong to types rather than static members of specific objects. Static modifiers can be used for classes, fields, methods, attributes, operators, events, and constructors, but cannot be used for types other than the indexer, destructor, or class. Classes, functions, and variables declared as static cannot reference instance methods or variables. In addition, once the class is added with a static modifier in C, all internal variables and methods must be static. Static variables and methods must be referenced by class names instead of instance objects.

So what are the differences between static keywords in php and C?

Declaration Scope

Compared with C #, PHP uses a wider range of static variables. We can not only add static modifiers before classes, methods, or variables, we can even add the static keyword to the variable inside the function. The variable with the static modifier is not lost even after the function is executed. That is to say, the variable still remembers the original value when the function is called the next time. For example:

<?phpfunction test(){    static $var1=1;    $var1+=2;    echo $var1.' ';}test();test();test();?> 

The running result is as follows:

3 5 7

Note that the value assignment operation of a variable will only be called during the first initialization of the variable. This operation will not be called during subsequent function execution.

Since functions in PHP are also a first-class citizen, unlike C #, we can directly define functions and call them directly anywhere in the code, which is somewhat similar to javascript. Therefore, static variables of a function are more useful than defining global variables. They can avoid conflicts caused by repeated definitions of variables. Since the function in C # cannot be directly defined and called, it must be embedded in the class. Therefore, if the function requires static variables, we only need to define them in the class to achieve the same effect.

Call Method

In C #, the method for calling static members is very simple and consistent, because static members do not belong to instance objects, so whether it is a method or a variable, C # access to its static members is by class name. method (variable. In C #, static functions cannot be set as virtual methods or overwritten. PHP provides more flexible and diverse support.

First, we know that PHP calls the instance method through someobj-> someFun (), so can we call static functions through SomeClass-> someFun () like C () what about calling? The answer is no. in PHP, the call to static members can only be done through:, for example, SomeClass: someFun ().

<?phpclass TestC{    public static $var1=1;    public $var2=1;    function t1(){        self::$var1+=2;        echo self::$var1.' ';        echo $this->var2.' ';    }    public static function t2(){        self::$var1+=2;        echo self::$var1.' ';    }}$t=new TestC();$t->t1();TestC::t2(); ?>

The running result is as follows:

3 1 5

Another difference from C # Is that in methods in the class, if we need to call static variables, we must use the self: $ somVar static variable (note the $ symbol before the variable, instance variables are not required), while static methods are called as self: someFun () ($ is not required here ). For example.

In addition, the biggest difference with C # Is that in PHP, subclasses can cover static functions or variables of the parent class. (from the perspective of C # programmers, I think PHP makes things complicated.) The default self: staticFun () calls the static function of the subclass, what if we want to call the static variables of the parent class at this time? Here, PHP provides additional parents to call static members of the base class. For example:

<?phpclass Test1{    public static $var1=1;    public static function t2(){        self::$var1+=2;        echo self::$var1.' ';    }}class Test2 extends Test1{    static $var1=‘Hello’;    static function t2(){        parent::t2();        echo self::$var1.' ';    }}Test1::t2();Test2::t2();?> 

The running result is as follows:

3 5 'hello'

It is best that, based on the above example, we can easily think that the Child class can use the parent keyword to access the parent class. How can the parent class access the static method of the Child class? Here is another usage of static. If the scope before the called static method is changed to static, PHP calculates the final static method based on the inheritance hierarchy of the class. For example:

<?phpclass Test1{    function t1(){        static::t2();    }    public static function t2(){        echo self::'Test1 ';    }}class Test2 extends Test1{    static function t2(){        echo self::'Test2 ';    }}$t=new Test2();$t->t1();Test2::t2();?> 

The running result is as follows:

Test2 Test2

Here, t instance finds the final static method based on its instance and outputs test2.

Summary

From the above analysis, we can easily see that for the use of static members, PHP provides more powerful functions or flexibility than C #, But from my perspective, this flexibility is not necessarily better. From a certain perspective, if the inheritance hierarchy of the class is too complex, it may confuse me. Of course, the use of the same tool will be totally different for different people. Since PHP provides more diverse options, I believe that if appropriate, static in PHP may provide more powerful and easy-to-use methods than in C.

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.