PHP object-oriented programming (oop) learning notes (2)-static variable attributes and methods and latency binding-PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming (oop) learning notes (2)-static variable attributes and methods and latency binding. Static keywords are used to define static methods and attributes. Static keywords can also be used to define static variables and Static binding later. 1. Static variables staticvariable static variables are only used in Static keywords to define static methods and attributes. Static variables can also be used to define static variables and post-Static binding.

1,Static variable

Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. That is to say, the variable will still remember the original value during the next execution of this function. To define a variable as static, you only need to add the static keyword before the variable.

The code is as follows:


Function testing ()
{
Static $ a = 1;
$ A * = 2;
Echo $ a. "\ n ";
}
Testing ();
Testing ();
Testing ();
Testing ();
/**
* 2
* 4
* 8
* 16
* [Finished in 0.1 s]
*/

Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to abort recursion.

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 the 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. If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a class (but static methods are acceptable ). Static attributes cannot be accessed by objects through the-> operator. Within the class, we can use the scope limitation operator to access variables of different levels of scopes.

2.1,Static attributes

Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods. Static variables can be considered to belong to the entire 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.

The code is as follows:


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.1 s]
*
*/

$ This is the current instance of the class and a reference to the main object.

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.

The code is as follows:


Class MyObjectBase
{
Static function MyMethod ()
{
Static: MyOtherMethod ();
}
Static function mythermethod ()
{
Echo 'called from MyObject .';
}
}
Class MyExtendObject extends MyObjectBase
{
Static function mythermethod ()
{
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].

If a method does not contain the $ this variable, the 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

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

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.

Http://www.bkjia.com/PHPjc/788636.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/788636.htmlTechArticleStatic (static) keywords are used to define static methods and properties, static can also be used to define static variables and later static binding. 1. static variable is only in the local...

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.