Home > Backend Development > PHP Problem > What is the difference between abstract class and interface in php

What is the difference between abstract class and interface in php

青灯夜游
Release: 2023-03-06 21:20:02
Original
3002 people have browsed it

Difference: 1. Abstract classes can have attributes, ordinary methods, and abstract methods, but interfaces cannot have attributes, ordinary methods, and can have constants; 2. Abstract classes are declared before the class with the abstract keyword, and have Class is declared as a class, and interface is declared with interface, but it cannot be declared with class, because interface is not a class.

What is the difference between abstract class and interface in php

Recommended: "PHP Video Tutorial"

For object-oriented development, abstract classes and interfaces are two things It is more difficult to understand. The following article will introduce to you the difference between abstract classes and interfaces in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Abstract class: It is based on a class. It is a class in itself. It is just a special class that cannot be directly instantiated. Methods and attributes can be defined in the class. Similar to templates, after specification, subclasses can implement detailed functions.

Interface: Mainly based on method specifications, a bit like abstract methods in abstract classes, except that it is more independent than abstract methods. A class can be combined into a new class by combining multiple methods.

The difference between abstract classes and interfaces in php

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

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

3. There are differences in syntax

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

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

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

Abstract class:

  • An abstract class cannot be instantiated. Its role is to define (or partially implement) interfaces for all classes that inherit from it.

  • Use the abstract keyword to define an abstract class.

  • Methods and properties can be created in abstract classes just like in ordinary classes. In most cases, an abstract class contains at least one abstract method. Abstract methods are also passed through the abstract keyword. declared. Abstract methods only have method declarations and no method implementations. The method body cannot be written.

abstract class ShopProductWriter
{
    protected $product = [];

    /**
     * 抽象类中可以定义普通方法
     */
    public function addProduct($shopProduct)
    {
        $this->product = $shopProduct;
    }

    /**
     * 定义一个抽象方法,只有方法声明,没有方法实现
     */
    abstract public function write();
}
Copy after login
  • Any class that inherits from an abstract class must implement all abstract methods, otherwise it must be defined as an abstract class itself. Subclasses cannot just implement abstract methods, they must also adhere to method signatures. This means that implementation methods cannot have stricter access control than abstract methods. The implementation method also needs to have the same number of parameters as the abstract method, and the same type hints.

class XmlProductWriter extends ShopProductWriter
{
    /**
     * 任何继承自抽象类的类都必须实现所有的抽象方法
     */
    public function write()
    {
        echo 'XmlProductWriter';
    }
}
Copy after login

Interface:

  • Abstract classes allow us to provide some implementations, but interfaces are pure templates. Interfaces only define functions and cannot have implementations. There can be declarations of properties and methods, but not method bodies.

  • Use the interface keyword to declare an interface.

interface Price
{
    /**
     * 只有方法声明,没有方法实现
     */
    public function getPrice();
}
Copy after login
  • Any class that implements this interface must implement all methods defined by this interface, otherwise this class can only be declared as an abstract class.

  • You can use the implements keyword in the declaration of a class to make it implement the interface.

class ShopProduct implements Price
{
    protected $price;

    public function getPrice()
    {
        // TODO: Implement getPrice() method.
        return $this->price;
    }
}
Copy after login
  • An implementation class has the same type as the interface it implements.

  • A class can implement multiple interfaces while inheriting a parent class. The implements keyword is followed by the names of multiple interfaces, separated by commas.

Related recommendations: php training

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template