Abstract Class and Interface in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 09:54:18
Original
1713 people have browsed it

Abstract Class and Interface in PHP

I recently started learning PHP MySQL. Let’s record the key points in the learning process, and then consider writing a series of blogs on the process of developing the website.

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

Abstract Class

What is Abstract Class

The same as the concept of abstract class in C, a class containing pure virtual function (called abstract method in Java and Php) is 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 will throw error in php
Copy after login

The abstract class in PHP is the same as other oop languages. We use the keyword abstract to declare an abstract class. If you want to directly generate an object of this class, an error will be reported.

abstract class testParent
{
public function abc()
{
//body of your funciton
}
}
class testChild extends testParent
{
public function xyz()
{
//body of your function
}
}
$a = new testChild();
Copy after login

testChild inherits the abstract class testParent through the keyword extends, and then we can generate a testChild object.

Implement Abstract Method

Similar to pure virtual functions in C, we can only declare Abstract method in abstract classes, and we can only and must define it in subclasses.

Actually, this statement is not absolute, but for the convenience of memory, most textbooks say this. Let’s review the explanation of pure virtual functions in Effective C .

"Pure Virtual functions must be redeclared in the 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){
    // 缺省行为,将飞机飞到指定的目的地
}
Copy after login
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){
    // 将C型飞机飞到指定的地方
}
Copy after login
In fact, we made an inline call to the virtual method in derived class ModelA.

The fly I want to be in is divided into two basic elements:

The declaration part represents the interface (which this derived class must use)

The definition part reflects the default behavior (that derived classes may use, but only if they explicitly request it)


The above content is excerpted from "Effective C 55 Specific Practices to Improve Programming and Design" Item 34: Distinguish between interface inheritance and implementation inheritance

Let’s come back and continue discussing 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();
Copy after login

In abc, we declare an abstract method f1 using the keyword abstract. In PHP

Once you declare an abstract method in an abstract class, all subclasses that inherit this class must declare this method , otherwise, PHP will report an error.

abstract class parentTest
{
abstract protected function f1();
abstract public function f2();
//abstract private function f3(); //this will 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();
Copy after login

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

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

Interface

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

Interface will force users to implement some methods. For example, if there is a class that requires set ID and Name attributes, then we can declare this class as an interface, so that all derived classes that inherit from this class will be forced to implement the setId and setName operations

Interface in php

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

Like other oop languages, we use the keyword Interface to declare it.

In this interface we declare a method xyz, thenAny time, such a method xyz

must be declared in the subclass

class test implements abc
{
public function xyz($b)
{
//your function body
}
}
Copy after login

You can use the keyword implements to inherit from interface.

In the interface, you can only use public, but not 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
}
}
Copy after login
You can use the extends keyword to inherit interface, just like a class.

The template2 here will contain all the attributes of template1, so in the implements class abc of template2, you will have to implement function f1 and f2,


You can also extend multiple interfaces:

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
}
}
Copy after login

At the same time, your class can also implement multiple interfaces

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
}
}
Copy after login

But if two interfaces contain methods with the same name, your class will not be able to implement them at the same time.

Methods inherited from interface must have the same parameter specifications . For example, the following code is feasible:

interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($a)
{
echo $a;
}
}
Copy after login

But code like this will error:

interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1()
{
echo $a;
}
}
Copy after login

However, we do not need to name the parameters in the two methods with the same name. The following code is feasible:

interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($name)
{
echo $name;
}
}
Copy after login

同时,如果使用default value,你还可以改变参数的default value,下面的代码是可行的:

interface template1
{
public function f1($a = 20)
}
class test implements template1
{
public function f1($name  = ankur)
{
echo $name;
}
}
Copy after login


 

Abstract Class和Interface之间的不同:

1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.

在Abstract class中并非所有的method都必须是抽象的,但是在interface中所有的method都自动成为抽象的。就是在子类中必须声明和实现

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

multiple和multilevel inheritance,我不知道改怎么翻译更好,multiple inheritance意思是 在interface中,一个class可以同时implements好多个interface;但是在abstract classes中,只能extends一个class。

当然你extends的这个class可能又extentds别的class,这就是所谓的multilevel inheritance。

3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.

interface中的method必须是public的,但是在abstract class中可以是public或者protected。

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

在abstract class中你可以同时声明(declare)和定义(define)methodes,但是在interface中你只能定义那个methods

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/998009.htmlTechArticlePHP中的Abstract Class和Interface 最近开始学习 PHP+MySQ L,记录下学习过程中的重点内容吧,然后考虑把开发网站的过程也写一个系列Blog。 这篇...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!