PHP Object-oriented interface

Source: Internet
Author: User

Interface (interface) technology What is an interface?

First look at the abstract class:

Abstract class class name {

attribute 1;

attribute 2;

.....

non-abstract method 1;

non-abstract method 2;

......

Abstract method 1;

Abstract Method 2;

......

}

It is envisaged that the "real members" of the above abstract classes be deleted, that is, those non-abstract members. Then, in the abstract class, there is only abstract method;

Abstract class class name {

Abstract method 1;

Abstract Method 2;

......

}

Thus, it can be understood as: This abstract class,"too abstract", almost nothing to do, just let others do what.

So:

interface is such a "natural not real" guy:

interface, is the stipulation, inside can only put "abstract method" and "constant" a similar class of grammatical structure;

It can be seen that an interface is a grammatical structure that is "more abstract than abstract class".

Interface (interface) definition form:

Interface Interface name {

constant 1;

constant 2;

.....

Abstract method 1;

Abstract Method 2;

.....

}

Description

1, visible, interface, only constants (interface constants) and abstract methods of the two members;

2, the interface constant is used as: interface name:: constant name;

3, the abstract method in the interface, do not use the abstract adornment, do not need to use the access control modifier, because its natural is public

Why do I need an interface?

The idea of object-oriented programming is a description of the "Real World" (simulation)!

The real world is often more inherited;

But:

In order to reduce the complexity of the relationship between classes and classes, the language is designed to be single-inheritance;

However, it is impossible to express the multi-inheritance characteristics of the real world;

The

Interfaces are a complement to the relationships between classes that do not have multiple inheritance;

because: Interfaces can implement "multiple inheritance"--but this is not called inheritance, but is called "implementation";

That

interface 1-->> class 1;

This is called: Class 1 implements interface 1;

its essence, in fact, is class 1 , with the interface 1 "feature information";

form of Use:

The form is:

Class name implements interface name 1, interface name 2, .... {

the definition of the class.

}

Here, called, the class implements the interface.

Among them, the interface and the interface, can also inherit, and the inheritance between the classes:

Interface Interface 1 extends interface 2{

member definition of an interface;

}

a large summary of classes and interfaces

PHP Object-oriented interface

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.