PHP object-oriented later static binding function introduction _ PHP Tutorial

Source: Internet
Author: User
PHP Object-Oriented Introduction to static binding later. PHP object-oriented static binding later this article will introduce PHP static binding later, it is mainly used to solve the class that references static calls within the inheritance scope. First, let's take a look at the PHP Object-Oriented Introduction to the later static binding function

This article will introduce PHP static binding later, which 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 ::

In this article, we will introduce the static binding function of PHP later. it is mainly used to solve the class that references static calls within the inheritance scope. First let's take a look...

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.