Tutorial on AbstractClass and Interface_PHP in PHP

Source: Internet
Author: User
Tags learn php
AbstractClass and Interface in PHP. The AbstractClass and Interface in PHP recently started to learn PHP + MySQL, record the key content in the learning process, and then consider writing a series of blogs into the website development process. Abstract Class and Interface in PHP

Recently started learningPHP + MySQL. record the key content in the learning process, and then consider writing a series of blogs into the website development process.

This blog mainly introducesAbstract ClassAndInterface.

Abstract Class what is Abstract Class (Abstract Class)

Similar to the abstract class concept in C ++Pure virtual functions(Java and Php are calledAbstract methodIs called Abstract Class. We sometimes call abstract ClassBase classBecause the 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

Abstract class and others in PHP OopThe same language, we use keywords AbstractTo declare an abstract class. if you want to directly generate the class object, an error is returned.

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

Similar to pure virtual functions in C ++, Abstract methods can only be declared in Abstract classes, but must be defined in subclasses.

In fact, this statement is not absolute, but in order to facilitate memory, most of the textbooks are like this. let's review it.Valid C ++To explain the pure virtual function.

"Pure Virtual functions must be declared again in 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 aircraft to a specified place}
In fact, we Derived classModelA creates a virtual method Inline call.

The fly is divided into two basic elements:

Part of its declaration is the interface (this derived class must be used)

The definition part of the code changes to the default behavior (it may be used by the derived class, but only when they explicitly submit an application)


The preceding content is excerpted fromObjective C ++ 55 specific practices for improving procedures and designCla34: differentiate interface inheritance and implement inheritance

Let's come back and continue to discussAbstract method.

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 a 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 method.Otherwise, php reports 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();

The code above shows that an error will be reported when declaring a private abstract method, because private method can only be used in the current class.

Note that in abstract class, the f1 function isProtectedBut in subclass, we can declare it as public. 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 the user to implement some methods. For example, if the set ID and Name attributes must be specified in a class, we can declare this class as interface, in this way, all the derived classes inherited from this class will be forced to implement the setId and setName operations.

Interface in php

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

Like other oop languages, we use keywords Interface.

In this interface, we declare a method xyzAnytime, Subclass must declare such a method xyz

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

You can use keywords ImplementsTo inherit from the interface.

In the interface, you can only usePublicBut cannot use 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 ExtendsKeyword to inherit the interface, just like the class.

Here, template2 will contain all the attributes of template1, so in implements from template2 class abc, function f1 and f2 must be implemented,


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 be implementsMultipleInterface

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}}

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

The method inherited from the interface must beSame parameter specificationsFor example, the following code is feasible:

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

However, such code will go 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 methods as the same name. The following code is feasible:

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

At the same time, if the default value is used, you can change the default value of the parameter. the following code is feasible:

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


Abstract Class and Interface are different:

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

In Abstract class, not all methods must be Abstract, but all methods in the interface are automatically abstracted. Must be declared and implemented in the subclass.

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

Multiple and multilevel inheritance, I don't know how to translate it better. multiple inheritance means that in the interface, a class can implements multiple interfaces at the same time; but in the abstract classes, only one class can be extends.

Of course, this extends class may be another class of extentds, which is called multilevel inheritance.

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

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

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

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

Begin Class and Interface recently began to learn PHP + MySQ L, record the key content in the learning process, and then consider writing a series of blogs into the website development process. This article...

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.