Abstract differences in PHP and Interface

Abstract Class
and the concept of abstract classes in C ++ as containing pure virtual functions (Java and Php are called abstract method) class called Abstract Class.
Sometimes we also put abstract Class is called the base class, because the base class can not generate objects directly.

Code Example:

abstract class example
{
   public function xyz()
   {
    return 1;
   }
}
$a = new example();//this will throw error in php

Abstract class PHP oop in other languages, we use the abstract keyword to declare an abstract class, if you want to directly generate objects of this class will complain.
abstract class is to be inherited and is implemented, otherwise it will be meaningless.

Code Example:

abstract  class testparent 
{ 
    public  function ABC () 
    { 
        // body of the funciton your 
    } 
} 
class testChild the extends testparent 
{ 
    public  function ABC () 
    { 
        // body of your function 
    } 
} 
$ A = new new testChild (); // abstract class of successor and instantiated    

$ a = new testChild (); // abstract class and the successor instantiated
testChild inherits the abstract class testParent keyword extends, then we can generate an object of the testChild.

Achieve the abstract method in PHP

Sample code:

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 keyword abstract abstract method f1.
In PHP Once you declare an abstract method in the abstract class, then all inherited this class subclass must go to declare this method, otherwise, php error.

Sample code:

abstract class parentTest
{
    abstract protected function f1();
    abstract public function f2();
    //abstract private function f3(); //this will trhow error
}
class childTest extends parentTest
{
    public function f1()
    {
    //body of your function
    }
    public function f2()
    {
    //body of your function
    }
    protected function f3()
    {
    //body of your function
    }
}
$a = new childTest();

Affirming a private abstract method will of error, because private method can only be used in the current class.
Note that in the abstract class f1 function is protected, but in the subclass we can be declared public.
There is no one less than the visibility limit visibility by the public.

###############################################################

Interface
interface users will be forced to implement some method.
For example there is a class requirements must be set ID and Name these two properties,
then we can put this class declared as interface,
so that all inherited from the class of the derived class will be compulsory and must be implemented setId two operations setName

Code Example:

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

And other oop language, we use the keyword Interface declared.
In this interface which we declare a method xyz, then any time, subclass must declare such a method xyz

Sample code:

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

You can use the keyword implements to inherit from interface.
In the interface, you can only use public, protected and can not be used as private

Sample code:

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 extends keyword to inherit interface, like that class.
Template2 herein will contain all the attributes of template1, thus implements a self template2 class abc, must implement the function f1 and f2,
it may also be a plurality of interface extends

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

But if two interface method contains the same name, then your class will not be at the same time implement them.
Inherited from the interface of the method must have the same parameter specifications, for example, the following code is feasible

Code Example:

interface template1
{
    public function f1($a)
}
class test implements template1
{
    public function f1($a)
    {
        echo $a;
    }
}
//BUT  下面就会出现错误
interface template1
{
    public function f1($a)
}
class test implements template1
{
    public function f1()
    {
        echo $a;
    }
}    

However, we do not need to put two inside the method parameters named the same name, the following code is feasible

Code Example:

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

Meanwhile, if the default value, you can also change the default value of the parameter, the following code is feasible

Sample code:

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

Summed up the difference between Abstract Class and Interface:

1. Abstract class not all of the method must be abstract, but all automatically become abstract method in the interface. That must be declared and implemented in subclasses

2.multiple inheritance (multiple inheritance) means that the interface in a class can be a good number of simultaneously implements interface; but in abstract classes, the only extends a class.

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

4. In the abstract class you can simultaneously declare (declare) and definitions (define) methodes, but you can only define the interface methods

Reference source  php Chinese network

Guess you like

Origin www.cnblogs.com/xionghao/p/12077619.html