Static in PHP

Source: Internet
Author: User
Tags final inheritance variables static class variable access


A static member is a class variable that can be viewed as belonging to the entire class rather than to an instance of the class. Unlike a generic instance variable, a static member retains only one variable value, which is valid for all instances, that is, all instances share this member.

$this represents only the current instance of a class, and self:: Represents the class itself, cannot use this operator in code other than a 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 a method declared in the base class, but it calls a method that is always overridden in an extended class. Unlike $this, when using static variables, you must add the $ symbol after the scope qualifier.

In an extended class, a method defined in the base class is used with the parent scope call when the method of the base class is overridden. Static members can also belong to only the parent class. If you declare a member at the same time in a subclass and a parent class, you can also use Parant:: To access a variable in the parent class in a subclass. In this case, the static members of the parent class and the static members of the subclass hold different values.

You can statically access a member by writing the name of the class on the left side of the: operator to avoid creating an instance of the class. Not only does it omit the code that instantiates the class, but it is also more efficient because each instance of the class consumes a small fraction of the system resources.

When you use the:: operator to access a member variable, you need to pay attention again to the use of the $ symbol. Because PHP does not currently support the use of dynamic static variables, it means that variable static variables are not supported. When using the $this-> $var, the member being accessed is the value of the variable contained in $var. Instead of using the $ symbol to access a variable, you actually look for a constant of the class, and constants cannot be accessed through $this.

Static in PHP6:: Scope makes it no longer necessary to use Self:: and Parent::. When you want to point to a class that has the final implementation functionality, you can use static:: This qualifier calculates the members of the last class on the inheritance hierarchy immediately before the code executes. One process is called deferred binding, which allows us to override a static variable in a subclass, and we can also ask this final member from a function declared in the parent class.

Sometimes it may be necessary to create fields and methods that are shared by all class instances, which are related to all instances of the class, but cannot be invoked by any particular object. For example, suppose you want to write a class that tracks the number of Web visitors. You certainly don't want to reset the number of visitors to 0 each time you instantiate the class, you can set the field to static scope:

 
  ";
    /* Instantiate another visitors class. * *
    $visits 2 = new visitors ();
    Echo Visitors::getvisitors ().
"; ? >

Program Run Result:

1

2

Because the $visitors field is declared static, any changes to its value are reflected in all instantiated objects. Also note that static fields and methods should be referenced using the Self keyword and the class name, not through the this and arrow operators. This is because it is not possible to refer to a static field using the normal method, resulting in a syntax error.

You cannot use $this in a class to refer to a static field.

static variables

A static variable is a variable that exists only in the scope of a function, but the value of such a variable is not lost when the function is executed, that is, the variable will still remember the original value the next time the function is called. To define a variable as static, simply precede the variable with the static keyword.

In a class, the static keyword has two main usages, one for defining static members and one for defining static methods. Within a class, you can use the Scope qualifier (::) To access variables of different levels of scope.

static method

There is an important difference between static and Non-static methods: When you call a static method, you no longer need to have an instance of the class.

Static and Non-static methods use the principle: one is if a method does not contain $this variables, should be static method, if you do not need an instance of the class, you might also use a static class, so you can eliminate the work of instantiating the class. Alternatively, you cannot use the $this variable in a static method because the static method does not belong to a particular instance.



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.