PHP OPP and Mode (2)-Static variables, properties and methods, and delay binding

Source: Internet
Author: User

PHP Advanced Programming Learning Note 2014.06.10

This article discusses the static keyword, which he can use on variables, classes, and methods.

1. Static variables

a static variable is a variable that exists only in the scope of a function, but after the execution of the function, the value of the variable is not lost, that is, the variable will still remember the original value the next time the function is executed. to define a variable as static, simply precede the variable with the static keyword.

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

In this example, the testing () function saves the value of the $a variable internally after each execution. When the next testing () function is called, the value of the $a is restored, and the testing () function multiplies the value by 2 and prints. The initial default value of a variable is 1, which occurs only when the variable is initialized for the first time. This action is not called during each execution of the function.

2. Use of static elements in classes

There are two main uses of the static keyword in a class, one for defining static members, and the other for defining static methods. Inside a class, we can use scope-qualified operators to access variables of different levels of scope.

2.1. Static properties

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

classmyobject{ Public Static $a= 0; functionMyMethod () { self::$a+ = 2; EchoSelf::$a. "\ n"; }}$instance 1=NewMyObject ();$instance 1-MyMethod ();$instance 2=NewMyObject ();$instance 2-MyMethod ();/** * * 2 * 4 * [finished in 0.1s] **/

$this indicator is the current instance of the class.

Self:: Represents the class itself, using the self:: The scope qualifier must be followed by the $ symbol after the operator, which cannot be used in code outside the class, and it does not recognize its position in the inheritance tree hierarchy. When you use the self:: scope in an extension class, self can call a method declared in the base class, but it always calls a method that has been overridden in the extension class.

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

Static:: So that we no longer need to use Self:: and Parent::. When you want to point to a class that ultimately implements functionality, you can use static, which immediately calculates the members of the last class on the inheritance hierarchy before the code executes.

2.3. Static method

The rules for static methods are the same as static variables. You can use the Static keyword to mark a method as a static method, whereas a static method can be accessed by the name and scope of the class (::).

There is one important difference between static and non-static methods: When you call a static method, we do not need to have an instance of the class.

classmyobjectbase{Static functionMyMethod () {Static::Myothermethod (); }    Static functionMyothermethod () {Echo' Called from MyObject. '; }}classMyextendobjectextendsmyobjectbase{Static functionMyothermethod () {Echo' Called from Myextendobject. '; }}myextendobject:: MyMethod ();

The previous example code correctly calls the Myothermethod method in Myextendobject, outputting called from Myextendobject. [Finished in 0.1s].

It is very difficult to know when static methods should be used and when non-static methods should be employed. One of the principles is that if a method does not contain a $this variable, then this method should be a static method. If you do not need an instance of the class, you should also use a static class, which avoids the instantiation of the work. In addition, $this variables cannot be used in static methods, because static methods do not belong to a particular instance.

2.4. Delay binding

Static:: So that we no longer need to use Self:: and Parent::. When you want to point to a class that ultimately implements functionality, you can use static, which immediately calculates the members of the last class on the inheritance hierarchy before the code executes. This process is called deferred binding.

3. Summary

A static variable is a modified function variable whose value remains intact after a function has been executed. You can use the static keyword to create a static variable, and you can also provide a default initialization value.

The static keyword can also be used in classes to decorate properties and methods. When used on a property, it causes the property to no longer hold a value for an instance, but instead holds a value for the entire class itself, which can be shared among members.

To access a static method can be used (::), which is called the scope qualifier. The left side of this operator can be a class name or a predefined scope, including the self parent static. The right side of the operator is a static method, property.

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.