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 implement Class inheritance and Interface inheritance in php

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to achieve class inheritance and interface inheritance in php. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. PHP classes do not support multiple inheritance, that is, subclasses can only inherit one parent class, but support multi-level inheritance.

For example:

Class frist {public function _ construct () {echo "I am the first class." , "

Public function printer () {echo "frist", "

";} class seconds extends frist {} class third extends seconds {} $obj = new third (); $obj- > printer ()

Result: this is the first class. The second and third classes of frist do not override the printer () method, and the third class can call the printer () method when instantiated.

2. Inheritance permission problem

(1) the parent private method cannot be inherited, and the parent class is privatized

(2) the parent class protected method can be inherited, but cannot be called by the instantiated object. If the call produces an error of "Call to protected method frist::printer ()", the parent class puts the method in protected mode.

(3) the parent class public method can be inherited and called by the instantiated object, which belongs to the public method.

(4) the subclass can override the method of the parent class public or protected, and cannot re-private the method. If the same name is only redeclared in this class,

3. Interface class inheritance

(1) the interface supports multiple inheritance, and the methods of the interface class do not need to be implemented, such as:

Interface frist {public function ex1 ();} interface seconds {public function ex2 ();} interface third extends frist,seconds {public function ex3 ();} class fourth implements third {public function ex1 () {} public function ex2 () {} public function ex3 () {}} class fourth implements frist,seconds {public function ex1 () {} public function ex2 () {}}

(2) the method permission of the API class is public, and the default is public.

(3) the class that references the interface must implement all the methods of the interface, or it will report an error, such as "Class e contains 1 abstract method and must therefore be declared abstract or implement the remaining methods", that is, class e contains 1 abstract method, so you must declare the abstract or implement the rest of the methods.

4. Abstract class: as long as a method in a class is an abstract method, then the class is defined as an abstract class

(1) Abstract classes inherit abstract classes and cannot override parent class methods.

(2) Abstract class inheritance is an extension of the method of the parent class.

(3) all abstract methods must be overloaded in inherited derived classes before they can be instantiated.

(4) the general release of abstract class implementation can be called directly in non-abstract subclasses.

5. Similarities and differences between interface and abstract class (difference between reference abstract class and interface)

(1) the use of the interface is realized by the keyword implements, while the operation of the abstract class is implemented by using the class inheritance keyword exotends

(2) the interface has no data members, but abstract classes have data members, and abstract classes can encapsulate data.

(3) the interface has no constructor, and abstract classes can have constructors.

(4) the methods in the interface are of public type, while the methods in the abstract class can be decorated with private, protected, or public.

(5) A class can implement multiple interfaces at the same time, but can only implement one abstract class.

(6) similarity: nothing can be written in the body of a function, such as: function getName ();

On how to achieve class inheritance and interface inheritance in php is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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

Wechat

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

12
Report