Home  >  Article  >  Backend Development  >  PHP, how does a subclass implicitly call the method of the parent class?

PHP, how does a subclass implicitly call the method of the parent class?

WBOY
WBOYOriginal
2016-10-10 11:56:13963browse

Suppose, the parent class has

<code>protected $info = '';
public function info()
{
    $this->info = 'xxxx';
}
</code>

Subcategories include

<code>echo $this->info;
</code>

How to automatically execute the parent class's info() without changing the subclass code, so that the subclass can obtain the effect of $this->info ='xxxx'?

Reply content:

Suppose, the parent class has

<code>protected $info = '';
public function info()
{
    $this->info = 'xxxx';
}
</code>

Subcategories include

<code>echo $this->info;
</code>

How to automatically execute the parent class's info() without changing the subclass code, so that the subclass can obtain the effect of $this->info ='xxxx'?

There are basically no programming languages ​​with automatic execution solutions, because programming is done according to the programmer's wishes. Even if there is so-called "automatic execution" or "automatic completion", it is completed under the instruction or implementation of the programmer.

If you insist on achieving the effect you want without modifying the subclass, why not just set protected $info to xxxx?
If you have any difficulties and cannot set the value of $info at the beginning, you can also consider the magic method.

Statement:
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