PHP object-oriented-Introduction to the static binding function later-php instances

Source: Internet
Author: User
This article mainly introduces the PHP object-oriented static binding function later. This article will introduce the PHP static binding function later, which is mainly used to solve the class that references static calls within the inheritance scope, for more information about PHP static binding, see this article, It is mainly used to solve the class that references static calls within the inheritance scope..

First, let's 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 the definition, rather than the class in the running. To solve this problem, you may rewrite the status () method in the inheritance class. A better solution is to add the static binding function after PHP 5.3.

The Code is 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

It can be seen that static: does not point to the current class. In fact, it is calculated during running, and all attributes of the final class are forcibly obtained.

Therefore, we recommend that you do not use self: or static: in the future ::

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.