Interface interface, interface inheritance implements

Source: Internet
Author: User

In PHP, only inheritance from one class is supported, and it is not supported to inherit from two or more classes at the same time. The ability to inherit from two or more two classes is called multiple inheritance. PHP is designed to prohibit this functionality. The reason for this is to avoid the complexities of multiple classes. When you find that you need to inherit from two or more classes, you can take the method of the parent class apart and use the interface for the same purpose.

Interfaces are similar to classes, however, the interface can contain only method prototypes, and cannot contain any complete definition methods. This prevents method conflicts that can occur when using abstract classes, allowing multiple interfaces to be used on a given implementation class. However, since an interface cannot define a method with a full implementation, we must provide a non-abstract base class separately if we want to provide the default functionality for the inheritors.

Description: The base class, is the father, grandparents class, etc., derived classes, such as subclasses, grandchildren and so on.

1. Interface definition

Use Keyword: interface

Define an interface Ispeedinfointerface ispeedinfo{public   function Getmaximumspeed ();}

2. Use of the interface

Use keyword: implements

Define an extension class Fastcar, and call the interface Ispeedinfoclass Fastcar extends Car implements ispeedinfo{    function Getmaximumspeed () {        return;}    }

Description: Extend class Fastcar, Know Ispeedinfo interface, and car base class is not aware of Ispeedinfo interface.

Note: When we want to call two or more than two interfaces, we use commas to separate the two

Class Fastcar extends Car implements ispeedinfoa,ispeedinfob,ispeedinfoc{}

3. Calling methods in the interface

Call $a=new Fastcar (); Echo $a->getmaximumspeed ();

======= Full Code ======

1<?PHP2 //define an interface Ispeedinfo3 Interfaceispeedinfo{4     Public functiongetmaximumspeed ();5 }6 7 //define an abstract class car8 classcar{9     //any non-abstract methodTen } One  A //define an extension class Fastcar and call the interface Ispeedinfo - ClassFastcarextendsCarImplementsispeedinfo{ -     functiongetmaximumspeed () { the         return150; -     } - } -  +     //called -     $a=NewFastcar (); +     Echo $a-getmaximumspeed (); A?>
View Code

Interface interface, interface inheritance implements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.