Abstract class and interface in PHP

Source: Internet
Author: User
Tags abstract define object definition functions implement inheritance php and

Recently began to learn php+mysqL, recorded in the learning process of the key content, and then consider the development of the site's process also write a series of blogs.

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

Abstract Class

what is abstract class (abstract)

As with the abstract class concepts in C + +, classes that contain pure virtual functions (called abstractmethod in Java and PHP) are called abstract class. We sometimes call abstract class base class, because base class cannot directly generate objects.

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
Abstract class in PHP and other OOPLanguage, we use the keyword AbstractTo declare an abstract class, if you want to directly generate the object of this class, you will get an error.
Abstract class Testparent
{public
function abc ()
{
//body of your Funciton
}
}
class Testchild extends testparent
{public
function 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. Implement abstract method

Like pure virtual functions in C + +, we can only declare abstract method in an abstract class, but only and must define it in a subclass.

In fact, this statement is not absolute, but for the convenience of memory, most of the textbooks said so, let us review the effective C + + in the pure virtual function of the explanation.

"The Pure virtual function must be declared in derived class, but they can also have their own implementation."

Class airplane{public
:
    virtual void Fly (const airport& destination) = 0;
    ....
};

void Airplane::fly (const airport& destination) {
    //default behavior, flying aircraft to 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 designated place
}
In fact, we are derived classIn Modela, the virtual method makes a Inline Call

The fly to be divided into two basic elements:

The declaration part shows the interface (this derived class must be used)

The definition part becomes the vacancy-saving Act (which may be used by the derived class, but only if they make a clear application).


The above is excerpted from "Effective C + + improvement procedures and design of 55 specific practices" clause 34: Differentiating interface inheritance and implementing inheritance

Let's go back and discuss the implementation of 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 abstract class, all subclass that inherit this class must go to declare thismethod, otherwise, PHP will complain.

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 function
}
protected function F3 ()
{
//body of your function
}
}
$a = new childtest ();

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

Note that the F1 function is protectedin the abstract class, but we can declare it as public in subclass. 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, there must be a set ID and name attribute in one class, so we can declare this class as interface so that all the derived that inherit from this class Class will force you to implement the SetID and SetName two actions

Interface in PHP
Interface ABC
{public
function xyz ($b);
}

As with other OOP languages, we use the keyword InterfaceTo declare.

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

Class Test implements ABC
{public
function xyz ($b)
{
//your function body
}
}

You can use the keyword Implementsto inherit from interface.

In interface, you can only use public instead of using the 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 extendsKeyword to inherit interface, like class.

The template2 here will contain all the template1 attributes, so in implements from Template2 class ABC, you 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
} public
function F3 ()
{
//your function body
}
}

At the same time, your class can also 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 body
}
}

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

Method that inherits 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;
}
}

But such a code would be wrong:
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, 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)
{
echo $name;
}
}


The difference between Abstract class and interface:

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

Not all method in abstract class must be abstract, but all of the method in interface is automatically abstract. is to declare and implement in subclasses

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 meaning is in interface, a class can implements a lot of interface , but only one class can be extends in the abstract classes.

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

3. Method of PHP interface must is public only. Method in the 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 abstract class, you can define as as declare methods. But in interface A can only defined your methods.

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



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.