Fundamentals 8 min read

Understanding Polymorphism in PHP: From Basic Example to Static and Dynamic Polymorphism

This article explains the concept of polymorphism in computer science, analyzes a non‑polymorphic PHP product class, demonstrates how inheritance and method overriding create a flexible, extensible design, and also covers static polymorphism via method overloading with illustrative code examples.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Polymorphism in PHP: From Basic Example to Static and Dynamic Polymorphism

Polymorphism is a core concept in computer science that allows the same interface to operate on different types of objects, much like a universal remote whose button performs different actions depending on the target.

The article first examines a PHP <?php class Product { protected $name; protected $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function displayDetails($type, $extra) { if ($type == "clothing") { echo "Clothing: {$this->name}, Size: {$extra}, Price: {$this->price}\n"; } elseif ($type == "electronics") { echo "Electronics: {$this->name}, Brand: {$extra}, Price: {$this->price}\n"; } } } // 创建新的产品对象 $product_cloth = new Product("T-Shirt", 19.99); $product_electronics = new Product("iPhone15", 199.99); // 显示 XL 码服装的详细信息 $product->displayDetails("clothing", "XL"); // 显示 iPhone 品牌电子 $product->displayDetails("electronics", "iPhone"); ?> implementation, highlighting problems such as the need to modify the class each time a new product type (e.g., book) is added and the reliance on a generic $extra parameter that quickly becomes unmanageable.

To resolve these issues, the article rewrites the code using inheritance and method overriding, producing a more extensible design:

<?php

class Product {
    protected $name;
    protected $price;

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

    public function displayDetails() {
        echo "Product: {$this->name}, Price: {$this->price}\n";
    }
}

class Book extends Product {
    private $author;

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

    public function displayDetails() {
        echo "Book: {$this->name}, Author: {$this->author}, Price: {$this->price}\n";
    }
}

class Electronics extends Product {
    private $brand;

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

    public function displayDetails() {
        echo "Electronics: {$this->name}, Brand: {$this->brand}, Price: {$this->price}\n";
    }
}

class Clothing extends Product {
    private $size;

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

    public function displayDetails() {
        echo "Clothing: {$this->name}, Size: {$this->size}, Price: {$this->price}\n";
    }
}

function displayProductDetails(Product $product) {
    $product->displayDetails();
}

$book = new Book("The Great Gatsby", 15.99, "F. Scott Fitzgerald");
$electronics = new Electronics("Smartphone", 499.99, "Samsung");
$clothing = new Clothing("T-Shirt", 19.99, "M");

displayProductDetails($book);
displayProductDetails($electronics);
displayProductDetails($clothing);
?>

This version allows new product types to be added simply by extending Product and overriding displayDetails() , without altering existing code, thereby improving flexibility and maintainability.

The article also introduces static polymorphism (method overloading) with a PHP example:

<?php
class MathOperations {
    // 两个数字相加的方法
    public function add($num1, $num2) {
        return $num1 + $num2;
    }
    // 三个数字相加的方法重载
    public function add($num1, $num2, $num3) {
        return $num1 + $num2 + $num3;
    }
}
$math = new MathOperations();
echo $math->add(2, 3) . "\n";        // 输出:5
echo $math->add(2, 3, 4) . "\n";      // 输出:9 
?>

It explains that static polymorphism (compile‑time) lets the compiler decide which method to call based on the parameter list, while dynamic polymorphism (run‑time) is demonstrated through the overridden displayDetails() methods in the subclass hierarchy.

Overall, the article shows how polymorphism—both static and dynamic—enhances code abstraction, reduces redundancy, and makes software development more efficient and adaptable.

PHPOOPpolymorphisminheritancemethod overloadingmethod overriding
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.