Home > Backend Development > PHP Tutorial > Inheritance, polymorphism and interfaces: three major object-oriented features of PHP

Inheritance, polymorphism and interfaces: three major object-oriented features of PHP

WBOY
Release: 2023-05-11 16:00:02
Original
1451 people have browsed it

PHP is a server-side programming language that supports object-oriented programming (OOP) since PHP5. The core idea of ​​OOP is to encapsulate data and behavior in objects to improve the maintainability and scalability of the program. In PHP, object-oriented programming has three major characteristics: inheritance, polymorphism and interfaces.

1. Inheritance

Inheritance means that one class can inherit properties and methods from another class. The inherited class is called the parent class or base class, and the inherited class is called the subclass or derived class. Subclasses can inherit the properties and methods of the parent class and can override or extend them.

For example, we can define an animal class Animal, which has attributes $name and $color, and methods eat() and sleep(). Then we can define a dog class Dog, which inherits from the Animal class and adds a bark() method:

class Animal {
    protected $name;
    protected $color;
    
    public function eat() {
        echo "$this->name is eating.
";
    }
    
    public function sleep() {
        echo "$this->name is sleeping.
";
    }
}

class Dog extends Animal {
    public function bark() {
        echo "$this->name is barking.
";
    }
}

$dog = new Dog();
$dog->name = "Fido";
$dog->color = "brown";
$dog->eat(); // 输出: Fido is eating.
$dog->sleep(); // 输出: Fido is sleeping.
$dog->bark(); // 输出: Fido is barking.
Copy after login

Note that in the parent class, we use the keyword protected to define the attributes $name and $color. This means that they can only be accessed within parent and child classes and not directly outside the class. In the subclass, we have used the keyword public to define the bark() method, which means it can be accessed both inside and outside the class.

2. Polymorphism

Polymorphism means that an object can appear in multiple forms. In object-oriented programming, polymorphism means that a subclass can replace a parent class without affecting the correctness of the program.

For example, we can define a zoo class Zoo, which has a show($animal) method to display animal information. Now we can pass different animal objects to the show() method to achieve polymorphism:

class Zoo {
    public function show($animal) {
        $animal->eat();
        $animal->sleep();
    }
}

$dog = new Dog();
$dog->name = "Fido";
$dog->color = "brown";

$cat = new Cat();
$cat->name = "Fluffy";
$cat->color = "white";

$zoo = new Zoo();
$zoo->show($dog); // 输出: Fido is eating. Fido is sleeping.
$zoo->show($cat); // 输出: Fluffy is eating. Fluffy is sleeping.
Copy after login

In this example, we have added a new cat class Cat, which inherits from the Animal class and overrides eat ()method. We can pass dog and cat objects to the show() method, and since they are both subclasses of the Animal class, they can achieve polymorphism.

3. Interface

An interface is a specification that defines a set of methods but has no specific implementation. In PHP, a class can implement one or more interfaces to meet specific functional requirements.

For example, we can define an interface Speakable, which has a speak() method for outputting animal sounds. Then we can let the dog and cat classes implement this interface:

interface Speakable {
    public function speak();
}

class Dog extends Animal implements Speakable {
    public function bark() {
        echo "$this->name is barking.
";
    }
    
    public function speak() {
        $this->bark();
    }
}

class Cat extends Animal implements Speakable {
    public function meow() {
        echo "$this->name is meowing.
";
    }
    
    public function speak() {
        $this->meow();
    }
}

$dog = new Dog();
$dog->name = "Fido";
$dog->color = "brown";
$dog->speak(); // 输出: Fido is barking.

$cat = new Cat();
$cat->name = "Fluffy";
$cat->color = "white";
$cat->speak(); // 输出: Fluffy is meowing.
Copy after login

In this example, we define a Speakable interface, which has a speak() method. Then we let the Dog and Cat classes implement this interface and implement the speak() method respectively. This way we can call the speak() method on dog and cat objects without knowing their specific implementation.

Inheritance, polymorphism and interfaces are the three major features of PHP object-oriented programming. They allow us to better organize and manage code and improve code reusability and scalability. Understanding these features can give us a deeper understanding of PHP's object-oriented programming model.

The above is the detailed content of Inheritance, polymorphism and interfaces: three major object-oriented features of 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