If Classes are the blueprints for single items, Inheritance is the concept that allows you to create specialized blueprints based on a general one. Access Modifiers are the rules that control who (or what code) can see and change the data inside those blueprints.
1. Access Modifiers (Security Rules)
Access modifiers control the visibility of a class’s properties and methods. They are crucial for encapsulation, which is the OOP principle of hiding sensitive data and restricting direct manipulation.
| Modifier | Visibility | Can it be accessed from… |
public | Open. | …anywhere (inside or outside the class). |
protected | Family only. | …the class itself and any child classes (classes that inherit from it). |
private | Self only. | …the class itself only. Not accessible from child classes or outside the object. |
Example: Controlling Data Access
<?php
class Account {
public $accountNumber; // Can be read/set anywhere
protected $balance = 0; // Accessible only inside Account and its children (e.g., SavingsAccount)
private $transactionHistory = []; // Accessible only inside the Account class
// Public method to modify the protected balance
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
// The private history can only be updated internally
$this->transactionHistory[] = "Deposit of $amount";
}
}
// Public method to view the protected balance
public function getBalance() {
return $this->balance;
}
}
$userAccount = new Account();
$userAccount->accountNumber = "12345"; // OK: public
$userAccount->deposit(100); // OK: Public method called
// echo $userAccount->balance; // ERROR: Cannot access protected property outside the class!
?>
2. Inheritance (Code Reusability)
Inheritance is when a new class (the Child Class or Subclass) is created based on an existing class (the Parent Class or Superclass).
The child class automatically inherits (gets) all the properties and methods of the parent class (except those marked as private). This saves time and ensures consistency.
You use the extends keyword to establish inheritance.
Example: Manager Inherits from Employee
<?php
// PARENT CLASS (General Blueprint)
class Employee {
protected $salary; // Protected so child classes can access it
public $name;
public function __construct($name, $salary) {
$this->name = $name;
$this->salary = $salary;
}
public function getDetails() {
return "Employee: " . $this->name . " | Salary: $" . $this->salary;
}
}
// CHILD CLASS (Specialized Blueprint)
// The Manager class extends the Employee class, inheriting $name, $salary, and getDetails()
class Manager extends Employee {
public $department;
// The constructor must accept all parent properties plus its own
public function __construct($name, $salary, $dept) {
// Call the parent's constructor to handle $name and $salary
parent::__construct($name, $salary);
$this->department = $dept;
}
// This is a new method only available in Manager
public function manageTeam() {
return $this->name . " is managing the " . $this->department . " team.";
}
}
$manager = new Manager("Sarah Connor", 85000, "Logistics");
// Accessing inherited methods and properties:
echo $manager->getDetails() . "<br>"; // Inherited from Employee
echo $manager->manageTeam(); // Unique to Manager
// Output:
// Employee: Sarah Connor | Salary: $85000
// Sarah Connor is managing the Logistics team.
?>
3. The parent:: Keyword
Inside the child class, the parent:: keyword is used to call methods from the parent class, especially the constructor.
When a child class has its own constructor (__construct), it overrides the parent’s constructor. To ensure the parent’s initialization logic still runs, you must explicitly call parent::__construct() within the child’s constructor.
4. Method Overriding
Method Overriding is when a child class defines a method with the exact same name as one in its parent class. The child’s version of the method will execute instead of the parent’s version. This allows you to customize inherited behavior.
Example: Overriding getDetails()
If we added the following method to the Manager class, it would run instead of the parent’s getDetails():
// Inside the Manager class:
public function getDetails() {
// We can still call the parent's method for basic details
$parentDetails = parent::getDetails();
return $parentDetails . " | Department: " . $this->department;
}
📚 Next Steps
You have now covered the three main pillars of OOP: Encapsulation (Access Modifiers), Abstraction (Classes/Objects), and Inheritance. The final chapter in your advanced PHP series should cover other high-level structures for code organization.
