Home  >  Article  >  Backend Development  >  PHP design patterns code reuse strategy

PHP design patterns code reuse strategy

王林
王林Original
2024-05-07 13:45:01694browse

PHP code reuse strategies include: Inheritance: Subclasses inherit parent class properties and methods. Composition: A class contains instances of other classes or objects. Abstract class: Provides partial implementation and defines methods to be implemented. Interface: defines methods, no need to implement them.

PHP 设计模式代码复用策略

PHP Design Patterns: Code Reuse Strategy

Introduction

Code Reuse Utilization is an important principle in software development, which can reduce code duplication, improve development efficiency and code maintainability. PHP provides a variety of strategies for achieving code reuse, the most commonly used of which include:

  • Inheritance
  • Composition
  • Abstract Class
  • Interface

##Practical Case: Building an Animal Class Library

To illustrate these strategies, we take building an animal class library as an example.

Inheritance

Inheritance allows subclasses to inherit the properties and methods of the parent class. For example, we could create a Mammal class that inherits from the Animal class:

class Animal {
    protected $name;

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

    public function getName() {
        return $this->name;
    }
}

class Mammal extends Animal {
    protected $numLegs;

    public function __construct($name, $numLegs) {
        parent::__construct($name);
        $this->numLegs = $numLegs;
    }

    public function getNumLegs() {
        return $this->numLegs;
    }
}

Composition

Composition allows a class to contain instances of other classes or objects. For example, we can create a talking animal class by combining the animal class and the talking interface:

interface Speakable {
    public function speak();
}

class TalkingAnimal {
    protected $animal;
    protected $speakable;

    public function __construct(Animal $animal, Speakable $speakable) {
        $this->animal = $animal;
        $this->speakable = $speakable;
    }

    public function speak() {
        $this->speakable->speak();
    }
}

Abstract class

The abstract class only provides partial implementation, and Defines methods that subclasses must implement. For example, we can create an abstract animal class that contains common methods and require subclasses to implement specific methods:

abstract class AbstractAnimal {
    protected $name;

    public function getName() {
        return $this->name;
    }

    abstract public function move();
}

class Dog extends AbstractAnimal {
    protected $numLegs;

    public function __construct($name, $numLegs) {
        $this->name = $name;
        $this->numLegs = $numLegs;
    }

    public function move() {
        echo "The dog runs on $this->numLegs legs.";
    }
}

Interface

Interface defines a set of methods, But it is not required to be implemented. This allows classes to provide specific behavior by implementing interfaces. For example, we can create a removable interface:

interface Movable {
    public function move();
}

class Dog implements Movable {
    // Implement the move method
}

The above is the detailed content of PHP design patterns code reuse strategy. 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