Home  >  Article  >  Backend Development  >  Can php implement multiple interfaces?

Can php implement multiple interfaces?

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-06-02 10:29:411111browse

php can implement multiple interfaces. PHP can use the "implements" keyword to bind a class to one or more interfaces, and let the class implement all the methods defined in the bound interface.

Can php implement multiple interfaces?

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

PHP can implement multiple interfaces.

In PHP, use the "implements" keyword to bind a class to one or more interfaces, allowing the class to implement all methods defined in the bound interface.

The following is a sample code:

interface Car {
    public function setModel($name);
    public function getModel();
}
interface Vehicle {
    public function setColor($rgb);
    public function getColor();
}
class MiniCar implements Car, Vehicle {
    private $model;
    private $color;
    public function setModel($name) {
        $this -> model = $name;
    }
    public function getModel() {
        return $this -> model;
    }
    public function setColor($rgb) {
        $this -> color = $rgb;
    }
    public function getColor() {
        return $this -> color;
    }
}

In the above code, two interfaces "Car" and "Vehicle" are defined, each interface has abstract methods. Next, a class named "MiniCar" is defined, which implements both interfaces, so MiniCar implements all methods of these two interfaces.

The above is the detailed content of Can php implement multiple interfaces?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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