Home  >  Article  >  Backend Development  >  What is the difference between php abstract classes and interfaces?

What is the difference between php abstract classes and interfaces?

青灯夜游
青灯夜游forward
2020-07-16 16:36:552601browse

This article will introduce to you the difference between PHP abstract classes and interfaces. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is the difference between php abstract classes and interfaces?

Abstract class

Please check the document for the official description, the official description is below A simplified version of the description:

  • #Classes defined as abstract cannot be instantiated. Any class must be declared abstract if at least one method in it is declared abstract.

(Abstract classes can have no abstract methods, but abstract classes still cannot be instantiated) A method defined as abstract only declares its calling method (parameters), cannot be defined Its specific function is realized.

For example:

abstract class  AbstractClass
 {
     // 强制要求子类定义这些方法,且不能定义具体功能 注意没有大括号{}
     abstract protected function  getValue ();
     abstract protected function  prefixValue ( $prefix );

     // 普通方法(非抽象方法)
     public function  printOut () {
        print  $this -> getValue () .  "\n" ;
    }
}
  • When inheriting an abstract class, the non-abstract subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be consistent with the parent class same (or looser). For example, if an abstract method is declared as protected, then the method implemented in the subclass should be declared as protected or public, and cannot be defined as private.
  • In addition, the calling method of the method must match, that is, the type and the number of required parameters must be consistent. For example, if the subclass defines an optional parameter ($b in function eat($a,$b=1) is an optional parameter), but it is not included in the declaration of the abstract method of the parent class, then There is no conflict between the two statements. This also applies to constructors since PHP 5.4. Constructor declarations before PHP 5.4 could be different.

Supplement:

1. Abstract classes can have member attributes.

2. Someone asked: Can an abstract method be defined as private? The answer is no, because the purpose of the abstract interface is to abstract the class model for inheritance. It is defined as private and cannot be accessed externally. If the design purpose is not met, an error will be reported.

3. An abstract class can implement an interface, and does not need to implement its methods

abstract class Sutdent extends Human
{
    abstract private function study();
}
Fatal error: Abstract function Sutdent::study() cannot be declared private in ...

4. An abstract class can inherit an abstract class, and cannotoverwrite the abstract parent class abstract method. Such usage can be understood as an extension of abstract classes. Such as

abstract class  Human
{
    abstract function eat();
}

abstract class Sutdent extends Human
{
    abstract function study();
    //abstract function eat(); 若重写抽象父类的抽象方法eat()会报错 Fatal error: Can't inherit abstract function Human::eat() (previously declared abstract in Sutdent) in ...
}

Interface

1. Definition of interface

  • Using interface (interface), you can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.
  • The interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty.
  • All methods defined in the interface must be public. This is a characteristic of the interface. Protected and private will report an error (Fatal error: Access type for interface method).
  • Constant: Constants can also be defined in interfaces. Interface constants are used exactly the same as class constants, but cannot be overridden by subclasses or subinterfaces. (It is not recommended to use this method. I really can’t think of any meaning, and it is easy to be confused with abstract classes)
interface Play  
{  
    const LEVEL=10;  
    public function PlayLOL();  
    public function PlayFootball();  
}

2. Implementation of the interface

To implement an interface, use the implements operator. Non-abstract classes must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.

Additional:

  • You can inherit abstract classes and implement interfaces at the same time, and extends must be written in front.
  • Abstract classes implement interfaces, and there is no need to recreate the methods in them.
  • When implementing multiple interfaces, methods in the interfaces cannot have the same name.
  • Interfaces can also be inherited by using the extends operator.
  • To implement an interface, a class must use methods that are exactly the same as those defined in the interface. Otherwise a fatal error will result.
interface Play  
{  
    const LEVEL=10;  
    public function PlayLOL();  
    public function PlayFootball();  
} 

interface Read  
{  
    public function ReadNovel();  
} 
    
abstract class  Human
{
    abstract function eat();
}
//抽象类可以实现接口后不实现其方法,可以继承一个抽象类的同时实现多个接口注意必须要把extends语句写在implements前面,否则会报错
abstract class Sutdent extends Human implements Play,Read
{
    abstract function study();
}

#3. Interface inheritance

The interface can inherit another or multiple interfaces, use the extends keyword, multiple Separated by ',', but you cannot implement another interface, and of course you cannot inherit an abstract class (error when inheriting an abstract class: Fatal error: PlayGame cannot implement Human - it is not an interface in D:\11 \index.php on line 10

interface Play  
{   
    public function PlayFootball();  
}
interface PlayNew  
{   
    public function PlayFootballNew();  
}

interface PlayGame extends play,PlayNew
{  
    public function PlayLOL();  
}

Difference:

1. Use implements for interface inheritance, Abstract classes use extends.

2. Variables cannot be declared in interfaces, but class constants can be declared. Various variables can be declared in abstract classes

3. Interfaces have no constructors, but abstract classes can Yes

4. The methods in the interface are public by default, and the methods in the abstract class can be modified with public, protected, private

5. A class can inherit multiple interfaces, but it can only inherit An abstract class

Related recommendations: PHP tutorial

The above is the detailed content of What is the difference between php abstract classes and interfaces?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete