Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of PHP subclass extending parent Class Operation

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of the extended parent operation of the PHP subclass. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The details are as follows:

In PHP, there is a situation in which the subclass inherits the parent class, but needs to extend the properties and methods of the parent class. In this case, the subclass can overwrite the properties and methods with the same name as the parent class, but if the methods of the parent class contain a lot of content, such as hundreds of thousands of lines of code At this point, you only need to use the "parent class name:: method" or "parent:: method" to call the overridden method in the parent class, reload the parent class, and then add the statements that need to be extended.

Method rewriting

Override methods and access permissions

When the subclass overrides the method of the parent class, it should be noted that the access permission of the method overridden in the subclass must not be lower than that of the method overridden by the parent class. For example, if the access permission of a method in the parent class is protected, then the permission of the method overridden in the subclass is protected or public. If the method of the parent class is public permission, the method to be overridden in the subclass can only be public. In short, when overriding a method of a parent class in a subclass, it must be higher than the permission of the method that the parent class overrides.

Number of parameters when overridden

A subclass can have a different number of parameters than the parent class, as in the constructor below, an extra parameter $age is added.

In the above example, we extend the "method" by overriding it.

However, although doing this solves the problem we have mentioned above, in actual development, it is impossible for a method to have only one code or several codes. For example, the "say ()" method in the "Person" class contains 100 pieces of code. If we want to add a little bit of function to the original function of this method, we have to rewrite the original 100 pieces of code, plus several pieces of extended code. This is good, and in some cases, the methods in the parent class cannot see the original code, so how do you rewrite the original code at this time? We also have a solution, that is, in the method of the subclass, we can call the method overridden in the parent class, that is, take the original function of the overridden method and add a little of its own function. You can call the method overridden by the parent class in the method of the subclass through two methods:

One is to use the "class name::" of the parent class to call the overridden method in the parent class

One is to use the "parent::" method to call the overridden method in the parent class.

Extension of the method

The above description only reloads the properties and methods of the parent class, not overloading in the real sense. It can only be said that the subclass extends the parent class. In php, the word overloading also exists, but it is different from the overloading meaning in the general language-oriented language.

On the "PHP subclass extension parent operation example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report