SoFunction
Updated on 2025-03-09

Detailed explanation of the difference between PHP abstract class and interface

For object-oriented development, abstract classes and interfaces are difficult to understand; even for programmers with certain experience. Let me explain these two things based on my own understanding. If there is anything wrong, I hope I will give you some advice.

Abstract class:It is based on a class, it is a class itself, it is just a special class, and cannot be directly instanced. Methods and attributes can be defined in the class. Similar to templates, the subclass implements detailed functions after specification.

interface:The specifications based mainly on methods are a bit like abstract methods in abstract classes, but they are more independent than abstract methods. A class can be used to form a new class by combining multiple methods.

The similarities between abstract classes and interfaces:

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

2. All of them use classes to achieve relevant detailed work

3. Syntax, the abstract methods of abstract classes are the same as interfaces, and cannot have a method body, that is, the {} symbol.

4. All can be inherited. Interfaces can inherit interfaces to form new interfaces. Abstract classes can inherit abstract classes to form new abstract classes.

The 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 the abstract class, but there must be "abstract" methods in the interface.

3. There are differences in grammar

4. Abstract class is declared before the class with abstract keyword, and class is declared as a class. The interface is declared with interface, but class cannot be declared because the interface is not a class.

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

6. Abstract class uses the extends keyword to allow the subclass to inherit the parent class and implements detailed abstract methods in the subclass. The interface uses implements to enable ordinary classes to implement interfaces in the class, and the interface can implement multiple methods at one time, and use commas to separate each interface.

Each of their characteristics:

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

In an abstract class, even if all of them are specific methods, they cannot be instantiated. Only after a new class is created can the instance inherit the class.

Interfaces allow a class to implement multiple different methods at once

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 my own thinking and has not been applied in actual development, but this writing method is a bit strange. Combine abstraction with interface.

1. Combination of abstract classes and interfaces

<?php 
/*
 Writing this program is based on your own guesses and wants to implement a certain interface in an abstract class. 
 */ 
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 program can be executed normally
 After the normal class implements interface, it becomes an abstract class, which is like adding an abstract method to the abstract class directly.
 */

2. The combination of interface and inheritance

The parent class is a normal class. After the subclass is inherited, the interface is implemented in the subclass at the same time.

Question: Is this approach meaningful and is there such an application in actual development?

<?php
interface kk{ 
  public function say(); 
}
class a { 
  public function show(){ 
    echo 'I am a parent<br />'; 
  } 
}
class b extends a implements kk{ 
  public function say(){ 
    echo 'I inherit Class A and implement the say interface at the same time<br />'; 
  }
}
$b=new b(); 
$b-&gt;show();//I am a parent class$b-&gt;say();//I'm inheritingAkind,Implemented simultaneouslysayInterface

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links