PHP object 您所在的位置:网站首页 子类怎么覆盖父类的属性 PHP object

PHP object

2024-07-09 08:34| 来源: 网络整理| 查看: 265

In PHP, you will encounter such a situation. The subclass inherits the parent class, but you need to make certain attributes and methods of the parent class. extension, then the subclass can rewrite the properties and methods, covering the properties and methods with the same name as the parent class. However, if the content of the parent class's method is relatively large, such as hundreds or thousands of lines of code, then only Use "parent class name::method" or "parent::method" to call the overridden method in the parent class , the only thing is to reload the parent class, and then add the statements that need to be expanded.

Overriding of methodsCopy after loginOverriding methods and access rights

When a subclass overrides a method of the parent class, please note that the access rights of the method overridden in the child class must not be lower than the access rights of the overridden method of the parent class. For example, the access permission of a method in the parent class is protected, then the permission of the method overridden in the subclass must be protected or public. If the method of the parent class has public permission, the method to be overridden in the subclass can only be public. In short, when overriding a method of the parent class in a subclass, it must be higher than the permissions of the overridden method of the parent class.

Number of parameters when overriding

Subclasses can have a different number of parameters than the parent class, such as the following Constructor method In , an additional parameter $age is added.

Copy after login

In the above example, we have achieved the extension of "method" by overriding it. However, although doing this solves the problem we mentioned above, in actual development, a method cannot be just one code or several codes, such as the "say()" method in the "Person" class There are 100 lines of code in it. If we want to overwrite this method and retain the original functions and add a little more functionality, we have to rewrite the original 100 lines of code, plus a few expanded lines of code. This is still Okay, but in some cases, the method in the parent class cannot see the original code. How do you rewrite the original code at this time? We also have a solution, that is, in the subclass method, you can call the overridden method in the parent class. That is, you can take the original functions of the overridden method and add some of your own functions. There are two methods. Implement the method of calling the overridden method of the parent class in the method of the subclass:

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

Extension of the methodCopy after login

What is described above is just reloading the attributes and methods of the parent class. It is not overloading in the true sense. It can only be said that the subclass is reloading the parent class. Expanded, in PHP, the word overloading also exists, but the meaning of overloading is different from that in general language-oriented languages.

The above is the detailed content of PHP object-oriented - sample code sharing of subclass extending parent class (subclass reloading parent class). For more information, please follow other related articles on the PHP Chinese website!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有