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 Class inheritance relationship in PHP

2025-02-24 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 class inheritance relationship in PHP. 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.

In PHP, I often write a class, the class writes a common method, and then let the subclass inherit to get the corresponding function. Suppose there is a parent class like this:

one

Then, create a subclass to inherit him:

one

The constructor that I am the parent class will be output at this time! And I are the constructors of subclasses.

Suppose you define such a method in a subclass:

1 protected function say ($str ='') {2 echo'I am the say';3 of the subclass

Then, the sys () method you inherited from the parent class at this time will be overridden, so the result of the call is: I am the say of the subclass.

What if you define it that way?

1 public function say ($str ='') {2 echo'I am a subclass method oh ~'; 3}

It is OK to write in this way. PHP is different from other strongly typed languages in that the PHP rewriting method allows you to "publicly own" the rewritten method upward, but not to "privatize it downward". For example, if you define private here, you are bound to make an error, while a language like C++ is just the opposite.

PHP's thinking about this situation is that your father gave you a protected level of inheritance, which is in your hands at this time. Your father allows you to share with others, that is, public, but you are not allowed to hide it yourself, that is, private. You can't keep it by yourself.

At this point, you must ask, what if the parent class's say () is set to private?

The result is that the parent class has been privatized and the subclass cannot inherit at all, so your say () method in the subclass is up to you.

Another problem is that the parent class has defined an optional parameter in sys (), so what happens to you in the subclass with no parameter definition like the following?

Public function say () {echo '. I don't have any parameters.

The result will work fine, but there will be E_STRICT-level prompts. The reason is that the PHP standard is that the number of parameters must be aligned with the parent class, and of course, you can set the error level in php.ini.

This is the end of this article on "sample analysis of class inheritance relationships in PHP". 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 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