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.
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.
Let’s look at the code:
abstract class abc { public function xyz() { return 1; } } $a = new abc();//this will throw error in php
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();
testChild inherits the abstract class testParent through the keyword extends, and then we can generate a testChild object.
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){ // 缺省行为,将飞机飞到指定的目的地 }
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型飞机飞到指定的地方 }
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();
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();
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 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 abc { public function xyz($b); }
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 } }
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 } }
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 } }
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 } }
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; } }
interface template1 { public function f1($a) } class test implements template1 { public function f1() { echo $a; } }
interface template1 { public function f1($a) } class test implements template1 { public function f1($name) { echo $name; } }
interface template1 { public function f1($a = 20) } class test implements template1 { public function f1($name = ankur) { echo $name; } }
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