PHP object-oriented-Introduction to the static binding function in the later stage-Introduction to object-oriented functions-PHP Tutorial

Source: Internet
Author: User
PHP object-oriented static binding function introduction and object-oriented function introduction. PHP object-oriented static binding later. This article will introduce PHP static binding later, it is mainly used to introduce the static binding feature of PHP Objects referenced in the inheritance scope. It also introduces the object-oriented feature.

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 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 problem of referencing static binding within the inheritance scope...

Related Article

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.