Home > Backend Development > PHP Tutorial > php study notes - object-oriented (4) inheritance supplement

php study notes - object-oriented (4) inheritance supplement

WBOY
Release: 2016-07-29 08:59:43
Original
851 people have browsed it

It was mentioned before that every time a new object is created, the __construct method will be executed first. So when inheriting, should the _construct method of the parent class be executed first, and then the __construct method of the subclass be executed?
Let’s do an experiment:

<code><span><span>class</span><span>Father</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>"father has constructed"</span>;
    }
}
<span><span>class</span><span>Child</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>"child has constructed"</span>;
    }
}
<span>$c</span> = <span>new</span> Child();
</code>
Copy after login

The output results are as follows:
child has constructed
It shows that the __construct method of the parent class is not called when creating the subclass. Why is this? It turns out that this is using the overwrite mechanism in PHP. The constructor of the subclass is actually an override. The constructor of the parent class is executed, and the constructor of the subclass is executed.
So what will happen if the subclass does not write the __construct method? Let’s experiment:

<code><span><span>class</span><span>Father</span>{</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>echo</span><span>"father has constructed"</span>;
    }
}
<span><span>class</span><span>Child</span>{</span><span>public</span><span><span>function</span><span>show</span><span>()</span>{</span><span>echo</span><span>"dd"</span>;
    }
}
<span>$c</span> = <span>new</span> Child();
<span>$c</span>->show();

输出结果为:dd</code>
Copy after login

;

The output result is: dd
It means that the __construct() of the parent class is not inherited.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the object-oriented (4) inheritance supplement of PHP study notes, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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