Understanding Inheritance in PHP: A Practical OOP Tutorial
This article explains the core concept of inheritance in object‑oriented programming using PHP, illustrating how child classes extend parent classes, inherit properties and methods, avoid code duplication, and can override functionality, with complete code examples for workers and tax calculations.
The article begins with a personal analogy about inheriting traits from parents and uses it to introduce the programming concept of inheritance, focusing on PHP as the teaching medium.
Inheritance is described as a fundamental principle of object‑oriented programming that allows a subclass to acquire attributes and methods from a parent (super) class.
Two core concepts are defined: the "parent class" (or base class) and the "child class" (or subclass).
A concrete example starts with a Father class that declares a $name property and two public methods, allBusinesses() and allProperties(), each returning a string.
<?php
class Father
{
public string $name = "Father";
public function allBusinesses(): string{
return "All Businesses" . PHP_EOL;
}
public function allProperties(): string{
return "All Properties" . PHP_EOL;
}
}The Father class serves as the base class whose properties and methods will be inherited.
A Son class is then declared that simply extends Father without adding any new code.
<?php
class Son extends Father
{
}Because Son extends Father, it automatically gains access to allBusinesses() and allProperties(). The article shows how to instantiate a Son object, call the inherited methods, and print their results.
<?php
require_once 'Father.php';
require_once 'Son.php';
$son = new Son();
print $son->allProperties();
print $son->allBusinesses();The output is: All Properties<br/>All Businesses<br/> This demonstrates that inheritance enables code reuse and avoids duplication. To illustrate the practical benefit, the article presents a tax‑calculation scenario involving two worker types: GovernmentWorker and PrivateWorker. Both classes originally contain identical calculateTax() methods that compute 10% of the salary.
<?php
class GovernmentWorker
{
public string $name;
public int $salary = 10000;
public function calculateTax(): int
{
return $this->salary * 10 / 100;
}
} <?php
class PrivateWorker
{
public string $name;
public int $salary = 9000;
public function calculateTax(): int
{
return $this->salary * 10 / 100;
}
}To eliminate the duplicated method, a base Worker class is introduced that contains the shared calculateTax() implementation.
<?php
class Worker
{
public function calculateTax(): int
{
return $this->salary * 10 / 100;
}
}Both GovernmentWorker and PrivateWorker now extend Worker, inheriting the tax‑calculation logic while defining their own specific properties.
<?php
require_once 'Worker.php';
class GovernmentWorker extends Worker
{
public string $name;
public int $salary = 10000;
} <?php
require_once 'Worker.php';
class PrivateWorker extends Worker
{
public $name;
public int $salary = 9000;
}Usage examples show how to instantiate each worker, call calculateTax(), and print the results (1000 for the government worker and 900 for the private worker).
<?php
require_once 'GovernmentWorker.php';
$governmentWorker = new GovernmentWorker();
$tax = $governmentWorker->calculateTax();
print $tax; // 1000 <?php
require_once 'PrivateWorker.php';
$privateWorker = new PrivateWorker();
$tax = $privateWorker->calculateTax();
print $tax; // 900The article then discusses method overriding: if a policy change requires the government worker's tax rate to drop from 10% to 5%, the GovernmentWorker class can override calculateTax() with its own implementation.
<?php
require_once 'Worker.php';
class GovernmentWorker extends Worker
{
public string $name;
public int $salary = 10000;
public function calculateTax(): int
{
return $this->salary * 5 / 100;
}
}A test script creates a GovernmentWorker object, calls the overridden method, and prints the new result (500), confirming that overriding works as intended.
<?php
require_once 'GovernmentWorker.php';
$governmentWorker = new GovernmentWorker();
$tax = $governmentWorker->calculateTax();
print $tax; // 500In conclusion, the article emphasizes that inheritance promotes code reuse, reduces redundancy, and allows subclasses to modify or override inherited behavior to meet specific requirements.
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.
