Introduction to post-static binding function after PHP object-oriented, introduction to object-oriented function_PHP tutorial

WBOY
Release: 2016-07-13 09:53:40
Original
897 people have browsed it

Introduction to PHP's object-oriented post-static binding function, object-oriented function introduction

This article will introduce the PHP post-static binding function, it is mainly used to solve Reference the statically called class within the inheritance scope.

First look at the following example:

Copy code 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. This is because self:: depends on the class in which it is defined, not the running class. In order to solve this problem, you may override the status() method in the inherited class. A better solution is that PHP 5.3 added the function of late static binding.

Copy code 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:: no longer points to the current class. In fact, it is calculated at runtime, forcing all properties of the final class to be obtained.

Therefore, it is recommended not to use self:: in the future, but to use static::

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000251.htmlTechArticleIntroduction to PHP object-oriented post-static binding function, object-oriented function introduction This article will introduce PHP post-static binding function To introduce, it is mainly used to solve the problem of referencing static in the inheritance scope...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template