What is an abstract class

  • An abstract class is a special class that usesabstractmodified
  • Abstract classes cannot be instantiated, only inherited
  • Abstract methods cannot be set to useprivateModifier, because abstract classes cannot be instantiated, private members make no sense.
  • abstractMethods can also be modified by calling them abstract methods: the class of an abstract method must be an abstract class, and an abstract method cannot have a method body
  • If a subclass inherits an abstract class, either the subclass itself is an abstract class or it implements all of the abstract methods
  • Abstract classes are intended to normalize subclasses and are usually used in large or canonical projects
  • No member attributes are allowed in abstract classes

      
// Abstract class

abstract class Human{

    // Common method
    public function show(){}

    // Abstract methods, abstract methods exist in the class must be abstract class
    // Abstract methods cannot have method bodies
    public abstract function eat();
};

// The abstract class cannot be instantiated
// new Human();

// An abstract class inherits either an abstract class or implements all of its abstract methods.
class Man extends Human{
    // Implement abstract method eat
    public function eat(){}};// Can be instantiated normally
new Man();

// Abstract subclasses inherit abstract classes
abstract class Woman extends Human{
    public abstract function makeup();
}

abstract class Wo extends demo{
    // Implement abstract method eat
    public function eat(){}

    // Implement the abstract method makeup
    public function makeup(){}}// All methods of an abstract class can be instantiated only if they implement the abstract class and inherit the abstract class
new Wo();
Copy the code

What is an interface

PHP is single-inherited, and to address this, PHP introduced interfaces. An interface is a special kind of abstract class, and an abstract class is a special kind of class.

  • Interfaces are not classes, but have a class-like structure for the purpose of constraining classes.
  • Interfaces cannot be instantiated, but classes can implement interfaces.

      
interfaceThe interface name{}
classThe name of the classimplementsThe interface name{}	
Copy the code
  • Interfaces are used to standardize the project architecture, providing some required behavior

Interface member: A member defined inside an interface

  • Interface members are allowed to have only two classes
    • Interface constants:const
    • Public interface methods (normal and static)
  • Interface methods are abstract methods with no method body (not requiredabstractKeyword, because interface methods are abstract methods.
  • Classes that implement interfaces:
    • Interface constants accessible: Interface constants cannot be overridden
    • All interface methods need to be implemented (unless they are abstract classes themselves)
    • Interface method implementations do not allow additional control permissions (must be public)
  1. Interface members: Only common abstract methods and interface constants can be defined in an interface

      
interface Humen{
    // Interface constants
    const NAME = 'people';
    // Interface abstract methods
    public function eat();
    public static function show();
}
Copy the code
  1. Interface member methods must be implemented in subclasses or classes that are abstract, and interface constants can be used directly in the implementation class
// Implement interface: entity class
class Man implements Humen{
    // All the abstract methods in the interface must be implemented
    public function eat(){
        // You can access constants
       echo self::NAME;
    }
    public static function show(){}}// Implement interface: abstract class
abstract class Women implements Humen{};
Copy the code
  1. Classes that implement interface members do not allow you to override constants in the interface or add control rights to interface methods
class Women implements Humen{
    // Overwriting interface constants is not allowed
    const NAME = 'child';
    // No other access modifiers are allowed; public must be used
    private function eat();
};
Copy the code

Interface inheritance: An interface can be inherited by an interface

  • Interface Inherits the purpose of the interface
    • Implement the extension of interface members, enrich the interface content, so as to achieve a better specification of the implementation class
    • To form a complete interface architecture, let different levels of classes implement different levels of interfaces
  • Interfaces can inherit multiple interfaces at once
 interface A{}
 interface B{}
 
 // Interface inheritance
 interface C extends A{}
 // Interface multiple inheritance
 interface D extends A.B{}
Copy the code

Conclusion:

  • Interfaces can inherit from interfaces, and multiinherit
  • Interfaces are specifications designed to ensure underlying implementation in large projects, usually by abstract classes implementing interfaces, adding necessary member attributes, and then having the actual business classes inherit the abstract classes
  • Classes can implement interfaces when they inherit from other classes, without any relationship between the parent class and the interface

The difference between interfaces and abstractions

  • Abstract classes can have non-abstract methods, whereas interfaces can only have abstract methods
  • A class can implement multiple interfaces, and a class can only inherit one abstract method
  • The interface usage mode passesimplementsImplementation, while the abstract class usesextendsKeyword inheritance.
  • Member variables can be declared in abstract classes, whereas they cannot be declared in interfaces, but constants can be declared.
  • Interfaces have no constructors, whereas abstract classes can have constructors
  • Abstract methods in interfaces can only be modified with public, while constructors can be modified with public and protected
  • Abstract methods of abstract classes are usedabstractThe interface does not