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

The usage of parent:: keyword in PHP

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

Share

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

This article mainly introduces "the usage of the parent:: keyword in PHP". In the daily operation, I believe that many people have doubts about the usage of the parent:: keyword in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the usage of the parent:: keyword in PHP! Next, please follow the editor to study!

Preface

Recently in the THINKPHP development project, used the parent:: keyword, in fact, the parent:: keyword is often used in a PHP function, this is not only in THINKPHP project development, even a small enterprise site, there will be many of the same function functions, we can encapsulate into a parent class class, and then multiple subclasses directly inherit the common part of the parent class.

A method that uses parent:: in PHP5 to refer to the parent class.

Parent:: can be used to call member methods defined in the parent class.

The traceability of parent:: extends beyond the direct parent class.

Call the parent class method through parent::

The result of running the program:

The salary of the average employee is 4200.

The salary of the manager is: 5700

Private property of the parent class

This thing is very uncomfortable to explain.

The Private property cannot be inherited if the parent class has private properties. Then the methods of the parent class only serve the private properties of the parent class.

The following example seems strange that a property $sal is redefined in the subclass, but the system returns the property of the parent class.

The result of running the program:

PHP 5.2.9

3000

Change the property $sal of the parent class to protected, and the subclass overrides the properties of the parent class. There is only one $sal in memory.

The result of running the program:

PHP 5.2.9

5000

If you have studied java, you will find all this difficult to understand.

In Java, when a subclass is created, the properties and methods of the parent class are created in memory, and even the constructor is called.

This is not the case with PHP5. PHP5 calls the parent class with parent:: instead of parent- >, which is enough to show that PHP5 does not want the parent class to be created in memory. PHP5 wants to make inheritance easier than Java.

Just get used to it.

This call will overflow the PHP5.1.1. I don't know if there's anything wrong with the new edition.

It would be nice to change line 12 to this. Pay attention to the comparison.

Return parent:: getSal ()

Such code causes recursive operations, where the subclass calls the parent class's method, which in turn invokes the subclass method.

Return parent::$this- > getSal ()

At this point, the study of "the usage of the parent:: keyword in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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