Home  >  Article  >  Backend Development  >  How to use inheritance, polymorphism and interfaces in PHP

How to use inheritance, polymorphism and interfaces in PHP

王林
王林Original
2023-06-23 11:49:551318browse

PHP is an open source scripting language for server-side programming and has become one of the most popular and widely used programming languages ​​in the world. In PHP, inheritance, polymorphism, and interfaces are three important object-oriented programming concepts that allow developers to gain great advantages in code reuse, flexibility, and maintainability. This article will delve into these three concepts and introduce how to use them in PHP.

  1. Inheritance

Inheritance is one of the most basic concepts in object-oriented programming. It means that a class can inherit the properties and methods of another class and add custom properties and methods on this basis. Subclassing is achieved by extending a parent class (also called a superclass or base class), allowing the subclass to reuse the parent class's code and functionality.

In PHP, inheritance is done by using the keyword "extends". For example, the following code is a simple inheritance implementation:

class Person {
  // 定义一个方法
  public function sayHello() {
    echo "Hello, world!";
  }
}

class Student extends Person {
  // 定义一个新的方法
  public function sayGoodbye() {
    echo "Goodbye, world!";
  }
}

// 使用子类的对象
$student = new Student();
$student->sayHello();  // 输出 “Hello, world!”
$student->sayGoodbye();  // 输出 “Goodbye, world!”

In this example, we define a class named Person and define a method named sayHello() in it. We then created a subclass called Student and extended it to the Person class using the "extends" keyword. In the subclass, we added a new method called sayGoodbye(). Finally, we create an instance of the Student class named $student and call its methods.

It should be noted that when a subclass inherits a parent class, it can not only inherit its properties and methods, but also access non-private properties and methods. In fact, for private properties and methods, subclasses cannot access them even after inheriting from the parent class.

  1. Polymorphism

Polymorphism is another key object-oriented programming concept. It refers to the fact that the same method can behave differently under different circumstances. Behavior. Methods with polymorphism can automatically determine the actual method that should be called at runtime, which greatly improves the flexibility and scalability of the program.

In PHP, the way to achieve polymorphism is to use the keywords "abstract" and "interface". We can achieve polymorphism by defining abstract classes or interfaces and inheriting abstract classes or implementing interfaces.

2.1 Abstract class

An abstract class is a class that cannot be instantiated and can only be inherited as a superclass of other classes. An abstract class can define some abstract methods, which must be implemented in its subclasses. In this way we can achieve polymorphism through abstract classes.

The following is an example of using abstract classes to achieve polymorphism:

abstract class Shape {
  // 定义一个抽象方法
  abstract public function getArea();
}

class Circle extends Shape {
  private $radius;

  public function __construct($radius) {
    $this->radius = $radius;
  }

  // 实现抽象方法
  public function getArea() {
    return pi() * pow($this->radius, 2);
  }
}

class Square extends Shape {
  private $side;

  public function __construct($side) {
    $this->side = $side;
  }
  
  // 实现抽象方法
  public function getArea() {
    return pow($this->side, 2);
  }
}

// 创建一个圆
$circle = new Circle(2);
// 创建一个正方形
$square = new Square(2);

// 输出面积
echo $circle->getArea();  // 输出 "12.566370614359"
echo $square->getArea();  // 输出 "4"

In this example, we define an abstract class Shape and declare an abstraction named getArea() method. Then, we created two subclasses, Circle and Square, and implemented the getArea() method respectively. Finally, we create instances of a circle and a square and call their getArea() methods.

2.2 Interface

An interface is an abstract type that defines a set of method signatures (but does not implement these methods). A class can implement this interface to express the behavior defined by this interface. a commitment. In this way we can achieve polymorphism through interfaces as well. In PHP, we can use the keyword "interface" to define the interface and the keyword "implements" to implement the interface.

The following is an example of using interfaces to achieve polymorphism:

interface Animal {
  public function makeSound();
}

class Dog implements Animal {
  public function makeSound() {
    echo "Woof!";
  }
}

class Cat implements Animal {
  public function makeSound() {
    echo "Meow!";
  }
}

// 创建一个狗
$dog = new Dog();
// 创建一只猫
$cat = new Cat();

// 输出声音
$dog->makeSound();  // 输出 "Woof!"
$cat->makeSound();  // 输出 "Meow!"

In this example, we define an interface Animal and declare a method named makeSound(). Then, we created two subclasses Dog and Cat that implemented the Animal interface, and implemented the makeSound() method respectively. Finally, we create instances of a dog and a cat and call their makeSound() methods.

It should be noted that interfaces can only define abstract methods, and these methods must be implemented by classes that implement the interface.

  1. Summary

Inheritance, polymorphism and interface are three important object-oriented programming concepts, which are also widely used in PHP. Using inheritance, we can let one class inherit properties and methods from another class and add custom properties and methods on top of that. Using polymorphism, we can call methods with different behaviors through the same method name in different situations. Using an interface, you can define a set of method signatures and have these methods implemented by classes that implement the interface.

When we write PHP object-oriented programs, reasonable use of the three concepts of inheritance, polymorphism and interfaces can make our programs more flexible, scalable and easier to maintain.

The above is the detailed content of How to use inheritance, polymorphism and interfaces in PHP. 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