Introduction to PHP static binding function after object-oriented

Source: Internet
Author: User

This article will introduce PHP's late static binding function, which is primarily used to resolve classes that reference static calls within the inheritance scope.

First look at the following example:

The code is as follows:

Class Person

{

public static function status ()

{

Self::getstatus ();

}

protected static function GetStatus ()

{

echo "person is alive";

}

}

Class deceased extends person

{

protected static function GetStatus ()

{

echo "person is deceased";

}

}

Deceased::status (); Person is alive

Obviously, the result is not what we expected, because self:: Depends on the class in which it is defined, not the class in which it is running. To solve this problem, you might rewrite the status () method in an inherited class, and a better solution would be to add later static binding functionality after PHP 5.3.

Copy code code as follows:

Class Person

{

public static function status ()

{

Static::getstatus ();

}

protected static function GetStatus ()

{

echo "person is alive";

}

}

Class deceased extends person

{

protected static function GetStatus ()

{

echo "person is deceased";

}

}

Deceased::status (); Person is deceased

Visible, Static:: Does not point to the current class, in fact, it is computed in run, forcing to get all the properties of the final class.

Therefore, it is recommended that you do not use self again::, using static::

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.