Introduction to the Post-object static binding function of PHP, Introduction to Object-oriented features _php tutorial

Source: Internet
Author: User

Introduction to the Post-object static binding function of PHP, Introduction to Object-oriented function


This article describes the post-static binding functionality of PHP, which is primarily used to resolve classes that refer to static calls within an inherited scope .

First look at the following example:

Copy the Code code 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 was defined, not the class in which it was run. To solve this problem, you might override the status () method in the inheriting class, and a better solution would be to add post-static binding functionality after PHP 5.3.

Copy the 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 class that is currently in, in fact, it is evaluated in the run, forcing all properties of the final class to be obtained.

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

http://www.bkjia.com/PHPjc/1000251.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000251.html techarticle PHP Post-object-oriented static binding function introduction, object-oriented features introduced in this article will be the late PHP static binding features, it is mainly used to solve the scope of inheritance in the reference 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.