Can php implement multiple interfaces?

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-02 10:29:41
Original
1165 people have browsed it

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;
    }
}
Copy after login

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!

Related labels:
php
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