Static_PHP tutorial in php

Source: Internet
Author: User
Static in php. Static members in php are a type variable that can be viewed as a whole class rather than an instance of the class. Unlike general instance variables, static members only retain the static

A static member is a type variable that can be viewed as a whole class rather than an instance of the class. Unlike general instance variables, static members only retain one variable value, which is valid for all instances, that is, all instances share this member.

$ This indicates only the current instance of the class, while self: indicates the class itself. this operator cannot be used in code other than the class, in addition, it cannot identify its position in the hierarchy of the inheritance tree. That is to say, when the self scope is used in the extension class, self can call the methods declared in the base class, but it always calls methods that have been rewritten in the extension class. Unlike $ this, when using static variables, you must add the $ symbol after the scope qualifier.

In an extended class, when the base class method is overwritten, the parent scope is used to call the method defined in the base class. Static members can only belong to the parent class. If a member is declared in both the subclass and parent class, you can also use parant: To access the variables in the parent class in the subclass. In this case, the static members of the parent class and the static members of the subclass save different values.

You can write the class name on the left of the: Operator to access a member statically, so as to avoid creating class instances. The code of the instantiated class is omitted, and it is more efficient because each instance of the class occupies a small part of system resources.

When using the: Operator to access member variables, pay attention to the use of the $ symbol again. PHP currently does not support dynamic static variables, that is, variable static variables are not supported. When $ this-> $ var is used, the Accessed member is the value of the variable contained in $ var. Instead of using the $ symbol to access a variable, the variable actually looks for a constant of the class, and the constant cannot be accessed through $ this.

The static: scope mentioned in PHP6 makes it unnecessary to use self: And parent ::. When you want to point to the class of the final implementation function, you can use static:. this qualifier will calculate the last class member on the inheritance hierarchy immediately before the code is executed. One process is called delayed binding, which enables us to override a static variable in the subclass, and can also ask this final member from a function declared in the parent class.

Sometimes it is necessary to create fields and methods shared by all class instances. these fields and methods are related to all class instances, but cannot be called by any specific object. For example, suppose you want to write a class to track the number of web page visitors. You do not want to reset the number of visitors to 0 every time you instantiate this class. in this case, you can set this field to static scope:

 ";    /* Instantiate another visitors class. */    $visits2 = new visitors();    echo visitors::getVisitors()."
";?>

Program running result:

1

2

Because the $ visitors field is declared as static, any changes to its value will be reflected in all instantiated objects. Note that static fields and methods should be referenced using the self keyword and class name, rather than using the this and arrow operators. This is because it is impossible to reference static fields using the "normal" method, which may cause syntax errors.

$ This cannot be referenced as a static field in a class.

Static variables

Static variables exist only in the function scope. However, after the function is executed, the value of such variables will not be lost. that is to say, when the function is called the next time, the variable will still remember the original value. To define a variable as static, you only need to add the static keyword before the variable.

In a class, the static keyword has two main usage methods: one is to define static members and the other is to define static methods. Within the class, you can use the scope qualifier (: :) to access variables of different levels of scopes.

Static method

There is an important difference between static methods and non-static methods: when calling static methods, you no longer need to own class instances.

Usage principles of static and non-static methods: first, if a method does not contain the $ this variable, it should be a static method. if you do not need an instance of the class, you may also need to use static classes, this removes the need to instantiate classes. In addition, $ this variable cannot be used in static methods, because static methods do not belong to a specific instance.

A worker static member is a type variable that can be viewed as a whole class rather than an instance of the class. Unlike general instance variables, static members only keep one...

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.