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

How to deal with the inheritance of PHP class

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

Share

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

This article introduces the relevant knowledge of "how to deal with the inheritance of PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the object-oriented of PHP, we can inherit an existing class through a class, the inherited class is called the parent class or the base class, and the class that inherits the parent class is called the subclass. In fact, the subclass can be understood as an extension of the parent class, and the subclass can add new functions beyond the parent class. The subclass can also inherit the constructor of the parent class. When the subclass is instantiated, PHP will first call the constructor in the subclass, and then call the constructor of the parent class when the subclass does not have one.

Extends inheritance keyword

In PHP, we implement class inheritance through the extends keyword. Its syntax format is as follows:

Class subclass name extends parent class name {.}

What we need to note is that a class can only inherit from another class, but a class can inherit multiple classes. That is, a subclass can have only one parent class, but a parent class can have multiple subclasses.

Inherit public members

In PHP, all members of the parent class that are decorated with public can be inherited by the child class.

Next, let's take a look at the example, which is as follows:

Output result:

In the above example, you define a base class, use another class to inherit the base class, and try to use subclasses to call member methods in the base class. As a result, all members of the parent class that are modified with public can be inherited by the subclass.

Inherit protected members

In the above we mentioned that members that inherit public modifications can be inherited by subclasses, and there is a protected member, which modifies members that do not want to be accessed externally of the class, which can be understood as a protected member that can only be accessed within the class, and can also be accessed within the subclass. We can set a member function in the subclass to access this member.

Examples are as follows:

Output result:

From the above example, we can see that variables modified by protected can also be accessed within inherited subclasses.

Inherit private members

In a PHP class, members decorated by private cannot be accessed outside the class or by subclasses, which can be understood as private decorating to become private members of the class. To be exact, members decorated by private are not inherited by subclasses and cannot naturally be accessed by subclasses.

Examples are as follows:

The output result is incorrect, and when a member of the parent class modified with the private keyword is called in the subclass, the program will report an error and stop running.

This is the end of the content of "how to handle the inheritance of PHP classes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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