Home > Backend Development > PHP Tutorial > 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
Release: 2016-10-10 11:56:13
Original
1019 people have browsed it

Suppose, the parent class has

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

Subcategories include

<code>echo $this->info;
</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Subcategories include

<code>echo $this->info;
</code>
Copy after login
Copy after login

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.

Related labels:
php
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