Detailed explanation of the difference between PHP abstract class and interface

  • 2021-12-04 09:34:53
  • OfStack

For object-oriented development, abstract classes and interfaces are difficult to understand. This is true even for programmers with 1 experience. Below according to their own understanding to tell 1 under these two things, if there is anything wrong, but also hope to comment.

Abstract class: Based on the class, it is a class itself, but a special class, which can not be directly instantiated, and can define methods and attributes in the class. Similar to templates, subclasses are allowed to implement detailed functions after specification.

Interface: Mainly method-based specification, a bit like abstract methods in abstract classes, except that they are more independent of abstract methods. You can make a class form a new class by combining multiple methods.

Similarities between abstract classes and interfaces:

1. They are all used to declare a certain kind of things, standardize names, parameters and form modules, without detailed implementation details.

2. All the related details are realized through classes

3. Syntactically, abstract methods of abstract classes are similar to interfaces 1, and cannot have method bodies, that is, {} symbols

4. Inheritance can be used, interfaces can inherit interfaces to form new interfaces, and abstract classes can inherit abstract classes to form new abstract classes

Differences between abstract classes and interfaces:

1. Abstract classes can have attributes, ordinary methods and abstract methods, but interfaces cannot have attributes, ordinary methods and constants

2. There may not be abstract methods in abstract classes, but there must be "abstract" methods in interface 1

3. Grammatical differences

4. Abstract classes are declared in front of classes with abstract keywords, and class is declared as classes. Interfaces are declared with interface, but they cannot be declared with class, because interfaces are not classes.

5. The abstract method 1 of the abstract class must be declared with abstract, but the interface does not need it

6. Abstract class is to use extends keyword to let subclass inherit parent class, and then implement detailed abstract methods in subclass. And the interface is to use implements to let the ordinary class in the class to realize the detailed method of the interface, and the interface can realize multiple methods at one time, and separate each interface with commas

Their respective characteristics:

There may not be abstract methods in an abstract class, but a class with abstract methods must be an abstract class

In an abstract class, even if it is full of concrete methods, it cannot be instantiated. Only when a new class is created to inherit, can the instance inherit the class

Interface allows a class to implement multiple different methods at one time

The interface itself is abstract, but note that it is not an abstract class, because the interface is not a class, but its methods are abstract. Therefore, it is also abstract

Application and combination:

The following code is based on their own thinking, not in the actual development of the application, but this writing is a bit strange. Combine abstraction with interface.

1. Combination of abstract classes and interfaces


<?php 
/* 
 Writing this program comes from my own guess, and I want to realize something in an abstract class 1 Interface.  
*/ 
interface work{ 
  public function say(); 
}
abstract class a implements work{ 
  public function showlove(){ 
    echo 'love you<br />'; 
  } 
}
class b extends a{ 
  public function say(){ 
    echo 'hello, i m in b'; 
  } 
}
$k=new b(); 
$k->say();
/* 
 The above procedures can be executed normally 
 General class implements Interface, it becomes an abstract class, which is as if it directly adds to the abstract class 1 Abstract methods. 
*/

2. Combination of Interface and Inheritance

The parent class is an ordinary class. After the subclass inherits, the interface is implemented in the subclass at the same time.

Question: Does this approach make sense, and is there such an application in actual development?


<?php
interface kk{ 
  public function say(); 
}
class a { 
  public function show(){ 
    echo ' I am the parent class <br />'; 
  } 
}
class b extends a implements kk{ 
  public function say(){ 
    echo ' I'm inheriting A Class, while implementing the say Interface's <br />'; 
  }
}
$b=new b(); 
$b->show();// I am the parent class 
$b->say();// I'm inheriting A Class, while implementing the say Interface's 

Summarize


Related articles: