Abstract class and interface in PHP

Source: Internet
Author: User

Original: http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html

Recently began to learn php+mysqL, record the key content of the learning process, and then consider the development of the site process also write a series of blog.

This blog mainly introduces the difference between Abstract Class and Interface .

Abstract Classwhat is abstract class

Like abstract class concepts in C + +, classes that contain pure virtual functions (called abstract methodin Java and PHP) are called abstract class. We also sometimes call the abstract class base class, because the base class cannot produce objects directly.

abstract Class in PHP

Let's look at the code:

Abstract class Abc{public function xyz () {return 1;}} $a = new ABC ();//this'll throw Error in PHP


the abstract class in PHP, like other oop languages, uses the keyword abstract to declare an abstraction, and if you want to generate an object of that class directly, you will get an error.


Abstract class Testparent{public function abc () {//body of your funciton}}class Testchild extends Testparent{public functi On XYZ () {//body of your function}} $a = new Testchild ();


Testchild inherits the abstract class testparent through the keyword extends, and then we can generate a Testchild object. implementing the abstract Method

Similar to pure virtual functions in C + +, we can only declare an abstract method in a class of abstraction, only and must define it in a subclass.

In fact, this statement is not absolute, but in order to facilitate memory, most of the textbooks said that, we review the effective C + + in the pure virtual function of the explanation.

"Pure virtual functions must be re-declared in the derived class, but they can also have their own implementations"

Class Airplane{public:    virtual void Fly (const airport& destination) = 0;    ....}; void Airplane::fly (const airport& destination) {    //default behavior, fly the plane to the specified destination}
Class Modela:public Airplane{public:    virtual void Fly (const airport& destination)    {airplane::fly ( destination);}    ....}; Class Modelb:public Airplane{public:     virtual void Fly (const airport& destination);    ....}; void Modelb:: Fly (const airport& destination) {    //Fly the C-plane to the specified place}
In fact, we made an inline call to virtual method in the derived class Modela.

The fly that you want to be divided into two basic elements:

The declaration part shows the interface (the derived class must use)

The definition of the part of the act (which is derived class may use, but only when they explicitly apply)


The above is excerpted from the 55 specific practices of the effective C + + Improvement Program and design clause 34: Differentiate between interface inheritance and implementation inheritance


Let's go back and discuss the implementation of the abstract method in PHP.

Abstract class Abc{abstract protected function F1 ($a, $b);} Class XYZ extends abc{protected function F1 ($name, $address) {echo "$name, $address";}} $a = new XYZ ();

in ABC, we declare an abstract method F1 with the keyword abstract. in PHP

Once you declare an abstract method in the abstract class, all subclass that inherit this class must go to declare this method, otherwise PHP will error.


Abstract class Parenttest{abstract protected function F1 (); abstract public Function f2 ();//abstract Private Function F3 () ; This would trhow Error}class childtest{public function F1 () {//body of your function}public function F2 () {//body of your F Unction}protected function F3 () {//body of your function}} $a = new childtest ();

as you can see in the code above, declaring a private abstract method will be an error because the private method can only be used in the current class.

Notice that the F1 function in the abstract class is protected, but in subclass we can declare it as public. No any visibility are less restricted than public .

Interface

Interface in OOP enforce definition of some set of method in the class.

Interface will force the user to implement some method. For example, if you have a class that requires both the set ID and the name attribute, then we can declare the class as interface so that all the derived that inherit from this class Class will force the need to implement SetID and setname two operations

Interface in PHP

Interface abc{public function xyz ($b);}

as with other OOP languages, we use the keyword Interface to declare.

In this interface we declare a method xyz, and at any time the subclass must declare such a method xyz


Class Test implements Abc{public function xyz ($b) {//your function Body}}

You can use the keyword implements to inherit from interface.

In interface, you can use only public, not such as protected and private


Interface Template1{public function F1 ();} Interface Template2 extends Template1{public function f2 ();} Class ABC implements Template2{public function F1 () {//your function Body}public function F2 () {//your function Body}}
You can use the extends keyword to inherit interface, as if it were class.

The template2 here will contain all the properties of the template1, so the class ABC implements from TEMPLATE2 will have to implement function F1 and F2,


You can also extends multiple interface:

Interface Template1{public function F1 ();} Interface Template2{public function f2 ();} Interface Template3 extends Template1, Template2{public function F3 ();} Class Test implements Template3{public function F1 () {//your function Body}public function F2 () {//your function Body}publi C function F3 () {//your function Body}}

At the same time, your class can implements multiple interface

Interface Template1{public function F1 ();} Interface Template2{public function f2 ();} Class Test implments template1, Template2{public function F1 () {//your function Body}public function F2 () {//your function B Ody}}

But if two interface contain the same name method, then your class will not be able to implement them at the same time.


The method inherited from interface must have the same parameter specification , for example, the following code is possible:

Interface Template1{public function F1 ($a)}class test implements Template1{public function F1 ($a) {echo $a;}}

However, this code will make an error:

Interface Template1{public function F1 ($a)}class test implements Template1{public function F1 () {echo $a;}}

However, we do not need to name the parameters in the two method as the same names, the following code is possible:

Interface Template1{public function F1 ($a)}class test implements Template1{public function F1 ($name) {echo $name;}}

Also , if you use default value, you can also change the default value of the parameter, and the following code is possible:

Interface Template1{public function f1 ($a =)}class Test implements Template1{public function f1 ($name  = "Ankur") {E Cho $name;}}


the difference between Abstract class and interface:

1. In the abstract classes this isn't necessary that every method should be abstract. But the interface every method is abstract.

Not all method in abstract class must be abstract, but all method in interface is automatically abstracted. Is that it must be declared and implemented in a subclass.

2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance are possible in abstract classes.

Multiple and multilevel inheritance, I do not know how to translate better, multiple inheritance means in interface, a class can implements a lot of interface at the same time But in the abstract classes, only one class can be extends.

Of course, you extends this class may extentds other class, this is called multilevel inheritance.

3. Method of PHP interface must is public only. Method in abstract class in PHP could is public or protected both.

The method in interface must be public, but it can be public or protected in the abstract class.

4. In the abstract class you can define as well as declare methods. But the interface can only defined your methods.

You can declare (declare) and define (define) Methodes at the same time in the abstract class, but in interface you can only define that methods


Abstract class and interface in PHP

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.