PHP object-oriented programming and design patterns (2) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP object-oriented programming and design patterns (2 ). PHP advanced programming study notes this article discusses static keywords, which can be used in variables, classes, and methods. 1. static variables are variable PHP advanced programming study notes that only exist in the function scope

This article discusses static keywords, which can be used in variables, classes, and methods.

1. 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 executed 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.

function testing(){    static $a = 1;    $a *= 2;    echo $a."\n";}testing();testing();testing();testing();/** *    2 *    4 *    8 *    16 *    [Finished in 0.1s]*/

In this example, the value of $ a is saved internally after each execution of the testing () function. When the next testing () function is called, the value of $ a is restored. then, the testing () function will multiply the value by 2 and print it. The initial default value of the variable is 1. this assignment only occurs when the variable is initialized for the first time. This operation is not called during each function execution.

2. use of static elements in a class

In a class, the static keyword has two main usage methods: one is used to define static members and the other is used to define static methods. Within the class, we can use the scope limitation operator to access variables of different levels of scopes.

2.1 static attributes

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

class MyObject{    public static $a = 0;    function MyMethod()    {        self::$a += 2;        echo self::$a . "\n";    }}$instance1 = new MyObject();$instance1 -> MyMethod();$instance2 = new MyObject();$instance2 -> MyMethod();/** * * 2 * 4 * [Finished in 0.1s] * */

$ This indicator is the current instance of the class.

Self: indicates the class itself. the $ symbol must be added to the operator when the self: Scope qualifier is used. this operator cannot be used in code outside the class, in addition, it cannot identify its position in the hierarchy of the inheritance tree. When self: is used in an 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.

Parent: In the extension class, if you want to access the method of the base class when the base class method is overwritten, you can use parent ::

Static: so that we no longer need 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 in the inheritance hierarchy before the code is executed.

2.3 Static method

Static method rules are the same as static variables. The static keyword can be used to mark a method as a static method, and the static method can be accessed through the class name and scope limitation operator.

There is an important difference between static methods and non-static methods: when calling static methods, we do not need to own class instances.

class MyObjectBase{    static function MyMethod()    {        static::MyOtherMethod();    }    static function MyOtherMethod()    {        echo 'called from MyObject.';    }}class MyExtendObject extends MyObjectBase{    static function MyOtherMethod()    {        echo 'called from MyExtendObject.';    }}MyExtendObject::MyMethod();

The code in the above example will call the MyOtherMethod method in MyExtendObject correctly and output called from MyExtendObject. [Finished in 0.1 s].

It is very difficult to clearly know when static methods should be used and when non-static methods should be used. One principle is that if a method does not contain the $ this variable, this method should be a static method. If you do not need class instances, you should also use static classes to avoid instantiation. In addition, the $ this variable cannot be used in static methods, because static methods do not belong to a specific instance.

2.4 delayed binding

Static: so that we no longer need 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 in the inheritance hierarchy before the code is executed. This process is called delayed binding.

3. Summary

Static variables are modified function variables. after a function is executed, its values remain unchanged. You can use the static keyword to create static variables and provide a default initialization value.

Static keywords can also be used in classes to modify attributes and methods. When used in attributes, it saves a value for the entire class instead of a value for an instance. static attributes can be shared among members.

To access static methods, you can use (: :), which is called the scope qualifier. The left side of this operator can be a class name or a predefined scope. the predefined scope includes self parent static. The right side of the operator is a static method and attribute.

This article discusses static keywords, which can be used in variables, classes, and methods. 1. static variables exist only in function scope changes...

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.