Class inheritance and polymorphism in PHP

WBOY
Release: 2023-06-19 17:38:02
Original
1608 people have browsed it

As a powerful programming language, PHP provides rich object-oriented programming (OOP) capabilities, including class inheritance and polymorphism. It is important for PHP developers to understand the concepts of class inheritance and polymorphism and how to apply these concepts as these concepts help in writing more efficient and maintainable code.

Class inheritance is an OOP concept that allows one class (subclass) to inherit the properties and methods of another class (parent class). Subclasses can use the properties and methods of the parent class, and can also define their own properties and methods. This kind of inheritance of classes is called single inheritance, which means that a class can only inherit from one parent class.

In PHP, use the keyword "extends" to implement class inheritance. The following is a simple example:

class Animal {
   // 父类属性和方法
   public $name;
   public function __construct($name) {
      $this->name = $name;
   }
   public function makeSound() {
      echo "Animal is making sound";
   }
}
class Dog extends Animal {
    // 子类新增属性和方法
    public function bark() {
        echo "Dog is barking";
    }
}
Copy after login

In the above example, the Dog class inherits the Animal class and defines a new method bark(). Let's see how to instantiate the Dog class and call its inherited and new methods:

$dog = new Dog("Fido");
echo $dog->name; // 输出 "Fido"
$dog->makeSound(); // 输出 "Animal is making sound"
$dog->bark(); // 输出 "Dog is barking"
Copy after login

Subclasses can override inherited methods. If a method with the same name as the parent class is defined in the subclass, the method in the subclass will override the method in the parent class. The following is an example:

class Animal {
    public function makeSound() {
        echo "Animal is making sound";
    }
}

class Cat extends Animal {
    public function makeSound() {
        echo "Cat is meowing";
    }
}

$cat = new Cat();
$cat->makeSound(); // 输出 "Cat is meowing"
Copy after login

Polymorphism is another important concept of OOP, which allows different objects to have the same method name, but each object can implement the method in its own way. Polymorphism can be achieved through class inheritance because a subclass can override the methods of its parent class. The following is an example:

class Animal {
    public function makeSound() {
        echo "Animal is making sound";
    }
}

class Dog extends Animal {
    public function makeSound() {
        echo "Dog is barking";
    }
}

class Cat extends Animal {
    public function makeSound() {
        echo "Cat is meowing";
    }
}

function makeAnimalSound(Animal $animal) {
    $animal->makeSound();
}

$dog = new Dog();
$cat = new Cat();

makeAnimalSound($dog); // 输出 "Dog is barking"
makeAnimalSound($cat); // 输出 "Cat is meowing"
Copy after login

In the above example, we defined a makeAnimalSound() function that accepts a parameter of type Animal. Regardless of the actual instance type passed to the function, its own makeSound() method will be called. This is the effect of polymorphism.

In PHP, class inheritance and polymorphism are very powerful and flexible OOP concepts. They make the code more modular and easier to maintain, and they also make the code more flexible and reusable. PHP developers are advised to familiarize themselves with these concepts and always apply them wherever possible to build better applications and code bases.

The above is the detailed content of Class inheritance and polymorphism 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