PHP design patterns code reuse strategy

王林
Release: 2024-05-07 13:45:01
Original
699 people have browsed it

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

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

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

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!