Inheritance and delayed static binding in php

Source: Internet
Author: User
: This article mainly introduces the inheritance and delay of static binding in php. For more information about PHP tutorials, see. The inheritance model of PHP has a long history problem, that is, it is difficult to reference the final state of the extension class in the parent class. This problem occurs before PHP5.3.

1 2 3 class ParentBase {
4 5 static $ property = 'parent value ';
6 7 public static function render (){
8 9 return self: $ property;
10 11}
12 13}
14 15 class Descendant extends ParentBase {
16 17 static $ property = 'scendant value ';
18 19}
20 21 echo Descendant: render ();
22

In this example, the self keyword is used in the render () method, which refers to the ParentBase class rather than the Descendant class. The final value of $ property cannot be accessed in the ParentBase: render () method. Because the self keyword will determine its scope during compilation rather than runtime. To solve this problem, PHP5.3 remedy this problem and re-specifies the role of the static keyword. in this case, rewrite the render () method in the subclass.

By introducing the delayed static binding function, you can use the static scope keyword to define the attributes of the class or the final value of the method, as shown in the code.

1 2 3 class ParentBase {
4 5 static $ property = 'parent value ';
6 7 public static function render (){
8 9 return static: $ property;
10 11}
12 13}
14 15 class Descendant extends ParentBase {
16 17 static $ property = 'scendant value ';
18 19}
20 21 echo Descendant: render ();
22


By using static scopes, PHP can be forced to search for the values of all attributes in the final class. In addition to this delayed binding behavior

The above introduces the inheritance and delay of static binding in php, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.