Understanding Inheritance in Object-Oriented Programming with PHP
This article explains how inheritance in object‑oriented programming can simplify PHP code by refactoring duplicated product and book classes into a reusable base class, demonstrating improved code reuse, maintainability, and polymorphic behavior through clear examples and explanations.
In object‑oriented programming (OOP), inheritance allows a subclass to acquire properties and methods from a superclass, promoting code reuse and modular design.
The article first presents a non‑inherited example where both Product and Book classes define their own $name, $price, and accessor methods, resulting in duplicated code.
class Product {<br/> protected $name;<br/> protected $price;<br/><br/> public function __construct($name, $price) {<br/> $this->name = $name;<br/> $this->price = $price;<br/> }<br/><br/> public function getName() {<br/> return $this->name;<br/> }<br/><br/> public function getPrice() {<br/> return $this->price;<br/> }<br/>}<br/><br/>class Book {<br/> protected $title;<br/> protected $author;<br/> protected $price;<br/><br/> public function __construct($title, $author, $price) {<br/> $this->title = $title;<br/> $this->author = $author;<br/> $this->price = $price;<br/> }<br/><br/> public function getTitle() {<br/> return $this->title;<br/> }<br/><br/> public function getAuthor() {<br/> return $this->author;<br/> }<br/><br/> public function getPrice() {<br/> return $this->price;<br/> }<br/>}<br/><br/>// Usage<br/>$cloth = new Product("T-shirt", 20);<br/>echo "Cloth: " . $cloth->getName() . ", Price: $" . $cloth->getPrice() . "<br>";<br/><br/>$book = new Book("The Great Gatsby", "F. Scott Fitzgerald", 15);<br/>echo "Book: " . $book->getTitle() . " by " . $book->getAuthor() . ", Price: $" . $book->getPrice();It then discusses the drawbacks of duplicating similar classes, especially when many product types are needed, leading to redundant, hard‑to‑maintain code.
To address this, the article refactors the code by creating a Product base class and letting Book extend it, adding only the specific $author property and related method, while reusing the inherited getName and getPrice methods.
class Product {<br/> protected $name;<br/> protected $price;<br/><br/> public function __construct($name, $price) {<br/> $this->name = $name;<br/> $this->price = $price;<br/> }<br/><br/> public function getName() {<br/> return $this->name;<br/> }<br/><br/> public function getPrice() {<br/> return $this->price;<br/> }<br/>}<br/><br/>class Book extends Product {<br/> protected $author;<br/><br/> public function __construct($name, $price, $author) {<br/> parent::__construct($name, $price);<br/> $this->author = $author;<br/> }<br/><br/> public function getAuthor() {<br/> return $this->author;<br/> }<br/>}<br/><br/>// Usage<br/>$cloth = new Product("T-shirt", 20);<br/>echo "Cloth: " . $cloth->getName() . ", Price: $" . $cloth->getPrice() . "<br>";<br/><br/>$book = new Book("The Great Gatsby", 15, "F. Scott Fitzgerald");<br/>echo "Book: " . $book->getName() . " by " . $book->getAuthor() . ", Price: $" . $book->getPrice();The revised implementation demonstrates how inheritance enhances reusability, reduces redundancy, and enables polymorphic behavior, laying a solid foundation for other OOP concepts such as abstraction and encapsulation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
