Access Modifiers in PHP
Access modifiers in PHP define the visibility of properties & methods within a class. They determine how & where these properties & methods can be accessed. PHP supports three access modifiers: public, protected, & private.
1. Public
Public properties & methods can be accessed from anywhere—both inside & outside the class. They are also accessible in child classes.
class ParentClass {
public $publicProperty = "I am public!";
}
class ChildClass extends ParentClass {
public function displayPublicProperty() {
echo $this->publicProperty; // Accessing public property
}
}
$child = new ChildClass();
$child->displayPublicProperty();

You can also try this code with Online PHP Compiler
Run Code
Output:
I am public!
In this example, the `publicProperty` is accessible in both the parent & child classes.
2. Protected
Protected properties & methods can only be accessed within the class itself & its child classes. They are not accessible outside these classes.
class ParentClass {
protected $protectedProperty = "I am protected!";
}
class ChildClass extends ParentClass {
public function displayProtectedProperty() {
echo $this->protectedProperty; // Accessing protected property
}
}
$child = new ChildClass();
$child->displayProtectedProperty();

You can also try this code with Online PHP Compiler
Run Code
Output:
I am protected!
Here, the `protectedProperty` is accessible in the `ChildClass` but cannot be accessed directly from outside the class.
3. Private
Private properties & methods can only be accessed within the class where they are defined. They are not accessible in child classes or outside the class.
class ParentClass {
private $privateProperty = "I am private!";
}
class ChildClass extends ParentClass {
public function displayPrivateProperty() {
// This will cause an error
echo $this->privateProperty;
}
}
$child = new ChildClass();
$child->displayPrivateProperty(); // Error: Cannot access private property

You can also try this code with Online PHP Compiler
Run Code
In this case, the `privateProperty` is only accessible within `ParentClass` & cannot be accessed in `ChildClass`.
Why Are Access Modifiers Important in Inheritance?
Access modifiers help in controlling how data is accessed & modified. They ensure data encapsulation, which is a key principle of OOP. By using `public`, `protected`, & `private`, you can decide what parts of your class should be exposed to other classes & what should remain hidden.
For example, if you want a property to be accessible in child classes but not outside the class hierarchy, you use `protected`. If you want a property to be accessible everywhere, you use `public`. If you want to restrict access to only the class where it is defined, you use `private`.
Accessing Parent Class Members
Sometimes, you may need to access a parent class’s methods or properties explicitly. This is done using the parent keyword.
Example: Accessing Parent Method
<?php
class Vehicle {
public function start() {
echo "Vehicle started.";
}
}
class Car extends Vehicle {
public function start() {
parent::start(); // Call the parent method
echo " Car engine is running.";
}
}
$car = new Car();
$car->start();
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Vehicle started. Car engine is running.
Here, the child class overrides the start() method but still uses the parent’s implementation with parent::start().
Types of Inheritance in PHP
Inheritance in PHP can be categorized into different types based on how classes inherit properties & methods. Understanding these types helps in designing better class hierarchies & writing more efficient code. The main types of inheritance are:
1. Single Inheritance
This is the simplest form of inheritance where a single class inherits from another class. PHP supports single inheritance, meaning a class can inherit from only one parent class.
class ParentClass {
public function parentMethod() {
return "This is a parent method.";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
return "This is a child method.";
}
}
$child = new ChildClass();
echo $child->parentMethod();
echo $child->childMethod();

You can also try this code with Online PHP Compiler
Run Code
Output:
This is a parent method.
This is a child method.
Here, `ChildClass` inherits from `ParentClass`, demonstrating single inheritance.
2. Multilevel Inheritance
In multilevel inheritance, a class inherits from another class, which in turn inherits from another class. This creates a chain of inheritance.
class GrandParentClass {
public function grandParentMethod() {
return "This is a grandparent method.";
}
}
class ParentClass extends GrandParentClass {
public function parentMethod() {
return "This is a parent method.";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
return "This is a child method.";
}
}
$child = new ChildClass();
echo $child->grandParentMethod();
echo $child->parentMethod();
echo $child->childMethod();

You can also try this code with Online PHP Compiler
Run Code
Output
This is a grandparent method.
This is a parent method.
This is a child method.
In this example, `ChildClass` inherits from `ParentClass`, which in turn inherits from `GrandParentClass`.
3. Hierarchical Inheritance
In hierarchical inheritance, multiple classes inherit from a single parent class. This means one parent class can have multiple child classes.
class ParentClass {
public function parentMethod() {
return "This is a parent method.";
}
}
class ChildClass1 extends ParentClass {
public function childMethod1() {
return "This is child method 1.";
}
}
class ChildClass2 extends ParentClass {
public function childMethod2() {
return "This is child method 2.";
}
}
$child1 = new ChildClass1();
$child2 = new ChildClass2();
echo $child1->parentMethod();
echo $child1->childMethod1();
echo $child2->parentMethod();
echo $child2->childMethod2();

You can also try this code with Online PHP Compiler
Run Code
Output
This is a parent method.
This is child method 1.
This is a parent method.
This is child method 2.
Here, both `ChildClass1` & `ChildClass2` inherit from `ParentClass`.
4. Multiple Inheritance (Not Supported in PHP):
Multiple inheritance allows a class to inherit from more than one parent class. However, PHP does not support multiple inheritance directly. Instead, you can achieve similar functionality using **traits**.
trait Trait1 {
public function method1() {
return "This is method 1.";
}
}
trait Trait2 {
public function method2() {
return "This is method 2.";
}
}
class ChildClass {
use Trait1, Trait2;
}
$child = new ChildClass();
echo $child->method1();
echo $child->method2();

You can also try this code with Online PHP Compiler
Run Code
Output
This is method 1.
This is method 2.
In this example, `ChildClass` uses two traits, `Trait1` & `Trait2`, to achieve functionality similar to multiple inheritance.
Why Are These Types Important?
Understanding the types of inheritance helps you decide how to structure your classes & relationships between them. For example:
- Use single inheritance for simple class hierarchies.
- Use multilevel inheritance for creating a chain of related classes.
- Use hierarchical inheritance when multiple classes share common functionality from a single parent.
- Use traits to simulate multiple inheritance when needed.
Inheritance Example: Child Class Inherits Public and Protected Members of Parent Class
Let’s take a more practical example to understand how a child class inherits public & protected members from a parent class. This will help you see how inheritance works in real code & how you can use it to build reusable & organized programs.
Now, look at the code:
class ParentClass {
// Public property
public $publicProperty = "I am a public property.";
// Protected property
protected $protectedProperty = "I am a protected property.";
// Public method
public function publicMethod() {
return "I am a public method.";
}
// Protected method
protected function protectedMethod() {
return "I am a protected method.";
}
}
class ChildClass extends ParentClass {
public function displayProtectedProperty() {
// Accessing protected property from the parent class
return $this->protectedProperty;
}
public function callProtectedMethod() {
// Accessing protected method from the parent class
return $this->protectedMethod();
}
}
// Creating an object of the ChildClass
$child = new ChildClass();
// Accessing public property & method
echo $child->publicProperty;
echo $child->publicMethod();
// Accessing protected property & method through child class methods
echo $child->displayProtectedProperty();
echo $child->callProtectedMethod();

You can also try this code with Online PHP Compiler
Run Code
Output
I am a public property.
I am a public method.
I am a protected property.
I am a protected method.
In this Code:
1. ParentClass:
- It has one public property (`$publicProperty`) & one protected property (`$protectedProperty`).
- It also has one public method (`publicMethod`) & one protected method (`protectedMethod`).
2. ChildClass:
- It extends `ParentClass`, meaning it inherits all the public & protected members of `ParentClass`.
- It defines two methods:
- `displayProtectedProperty()`: This method accesses the protected property (`$protectedProperty`) of the parent class.
- `callProtectedMethod()`: This method calls the protected method (`protectedMethod`) of the parent class.
3. Accessing Members:
- The public property & method of the parent class can be accessed directly using the child class object.
- The protected property & method cannot be accessed directly from outside the class. However, they can be accessed within the child class using methods like `displayProtectedProperty()` & `callProtectedMethod()`.
Key Points to Remember:
- Public Members: Can be accessed from anywhere, including outside the class & in child classes.
- Protected Members: Can only be accessed within the class itself & in child classes. They are not accessible from outside the class hierarchy.
- Private Members: Cannot be accessed in child classes or outside the class. They are restricted to the class where they are defined.
This example shows how inheritance helps you to reuse code & build a hierarchy of classes while maintaining control over the visibility of properties & methods.
Method Overriding Example
Method overriding is a feature in PHP that allows a child class to provide a specific implementation of a method that is already defined in its parent class. This means the child class can redefine a method inherited from the parent class to suit its needs. Let’s explore this concept with a detailed example.
For example:
class ParentClass {
public function greet() {
return "Hello from the ParentClass!";
}
}
class ChildClass extends ParentClass {
// Overriding the greet() method of the parent class
public function greet() {
return "Hello from the ChildClass!";
}
}
// Creating objects of both classes
$parent = new ParentClass();
$child = new ChildClass();
// Calling the greet() method
echo $parent->greet();
echo $child->greet();

You can also try this code with Online PHP Compiler
Run Code
Output
Hello from the ParentClass!
Hello from the ChildClass!
In this Code:
1. ParentClass:
- It defines a method called `greet()` that returns a simple message: `"Hello from the ParentClass!"`.
2. ChildClass:
- It extends `ParentClass` & overrides the `greet()` method.
- The overridden `greet()` method in `ChildClass` returns a different message: `"Hello from the ChildClass!"`.
3. Method Overriding:
- When you create an object of `ParentClass` & call the `greet()` method, it executes the method defined in `ParentClass`.
- When you create an object of `ChildClass` & call the `greet()` method, it executes the overridden method defined in `ChildClass`.
Why Use Method Overriding?
Method overriding is useful when you want to:
- Modify or extend the behavior of a method inherited from a parent class.
- Provide a specific implementation of a method in the child class while keeping the method name the same.
- Achieve polymorphism, where the same method can behave differently based on the object calling it.
Accessing the Parent Class Method from the Child Class
Sometimes, you may want to call the parent class method from within the overridden method in the child class. You can do this using the `parent::` keyword.
Example:
class ParentClass {
public function greet() {
return "Hello from the ParentClass!";
}
}
class ChildClass extends ParentClass {
public function greet() {
// Calling the parent class method
$parentMessage = parent::greet();
return $parentMessage . " And hello from the ChildClass!";
}
}
$child = new ChildClass();
echo $child->greet();

You can also try this code with Online PHP Compiler
Run Code
Output:
Hello from the ParentClass! And hello from the ChildClass!
In this example:
- The `greet()` method in `ChildClass` calls the `greet()` method of `ParentClass` using `parent::greet()`.
- It then appends its own message to the result.
Key Points to Remember:
- Method overriding allows a child class to provide its own implementation of a method defined in the parent class.
- The overridden method in the child class must have the same name & parameters as the method in the parent class.
- You can call the parent class method from the child class using the `parent::` keyword.
Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance where multiple child classes inherit from a single parent class. This means one parent class can have multiple child classes, each of which can have its own unique properties & methods while sharing common functionality from the parent class. Let’s understand this concept with a detailed example.
For example:
class Animal {
public function eat() {
return "This animal is eating.";
}
public function sleep() {
return "This animal is sleeping.";
}
}
class Dog extends Animal {
public function bark() {
return "The dog is barking!";
}
}
class Cat extends Animal {
public function meow() {
return "The cat is meowing!";
}
}
// Creating objects of the child classes
$dog = new Dog();
$cat = new Cat();
// Accessing methods from the parent class
echo $dog->eat();
echo $dog->sleep();
echo $cat->eat();
echo $cat->sleep();
// Accessing methods specific to the child classes
echo $dog->bark();
echo $cat->meow();

You can also try this code with Online PHP Compiler
Run Code
Output
This animal is eating.
This animal is sleeping.
This animal is eating.
This animal is sleeping.
The dog is barking!
The cat is meowing!
In this code:
1. Parent Class (`Animal`):
- It defines two common methods, `eat()` & `sleep()`, which represent behaviors shared by all animals.
2. Child Class 1 (`Dog`):
- It extends the `Animal` class, inheriting the `eat()` & `sleep()` methods.
- It also defines its own unique method, `bark()`, which is specific to dogs.
3. Child Class 2 (`Cat`):
- It also extends the `Animal` class, inheriting the `eat()` & `sleep()` methods.
- It defines its own unique method, `meow()`, which is specific to cats.
4. Hierarchical Inheritance in Action:
- Both `Dog` & `Cat` classes share the common methods (`eat()` & `sleep()`) from the `Animal` class.
- Each child class also has its own unique methods (`bark()` for `Dog` & `meow()` for `Cat`).
Why Use Hierarchical Inheritance?
Hierarchical inheritance is useful when:
- You have a common set of functionalities that need to be shared across multiple classes.
- You want to avoid code duplication by defining common methods in a single parent class.
- You need to create specialized classes that share some behaviors but also have their own unique features.
Real-Life Analogy
Think of hierarchical inheritance like a family tree. The parent class is like a grandparent, & the child classes are like siblings. Each sibling (child class) inherits traits (methods & properties) from the grandparent (parent class) but also has their own unique traits.
Key Points to Remember:
- Hierarchical inheritance allows multiple child classes to inherit from a single parent class.
- It promotes code reusability by allowing common functionalities to be defined in the parent class.
- Each child class can have its own unique properties & methods in addition to the inherited ones.
Inheritance and UML
Unified Modeling Language (UML) is a standardized way to visualize the design of a system, including its classes & their relationships. Inheritance is one of the key relationships in UML, & it is represented using a specific notation. Let’s understand how inheritance is depicted in UML & how it helps in understanding class hierarchies.
UML Notation for Inheritance
In UML, inheritance is represented by a solid line with a hollow arrowhead pointing from the child class to the parent class. For example:
ParentClass
^
|
|
ChildClass
- The arrowhead points to the parent class, indicating that the child class inherits from it.
- The solid line represents the inheritance relationship.
Example: UML Diagram for Hierarchical Inheritance
Let’s take the previous example of hierarchical inheritance with the `Animal`, `Dog`, & `Cat` classes. Let’s see how it would look in UML:
Animal
^ ^
/ \
/ \
Dog Cat
- `Animal` is the parent class.
- `Dog` & `Cat` are child classes that inherit from `Animal`.
UML Diagram for Multilevel Inheritance
For multilevel inheritance, the UML diagram would look like this:
GrandParentClass
^
|
ParentClass
^
|
ChildClass
- `GrandParentClass` is the top-level parent.
- `ParentClass` inherits from `GrandParentClass`.
- `ChildClass` inherits from `ParentClass`.
Why Use UML for Inheritance?
UML diagrams are helpful because:
- They provide a visual representation of class relationships, making it easier to understand the structure of a program.
- They help in designing & planning the class hierarchy before writing the actual code.
- They serve as documentation for the system, making it easier for other developers to understand the design.
Example: UML Diagram with Method Overriding
Let’s take an example where a child class overrides a method from the parent class. Here’s how it would look in UML:
ParentClass
^
|
|
ChildClass
- In the `ParentClass`, you might have a method like `greet()`.
- In the `ChildClass`, the `greet()` method is overridden to provide a different implementation.
Key Points to Remember:
- UML uses a solid line with a hollow arrowhead to represent inheritance.
- The arrowhead points to the parent class, & the line connects it to the child class.
- UML diagrams are a great way to visualize & document class hierarchies & relationships.
Overriding Parent Methods
Method overriding occurs when a child class defines a method with the same name as a method in its parent class. This allows the child class to provide its implementation.
Example: Overriding Methods
<?php
class ParentClass {
public function greet() {
echo "Hello from ParentClass!";
}
}
class ChildClass extends ParentClass {
public function greet() {
echo "Hello from ChildClass!";
}
}
$child = new ChildClass();
$child->greet();
?>
Output:
Hello from ChildClass!
The child class completely replaces the parent class method with its own.
Final Keyword
PHP provides the final keyword to prevent a method or class from being overridden.
Example: Using final Keyword
<?php
class BaseClass {
final public function show() {
echo "This is a final method.";
}
}
class DerivedClass extends BaseClass {
// Cannot override the final method
}
$object = new DerivedClass();
$object->show();
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
This is a final method.
In this case, the show() method in the parent class cannot be overridden by the child class.
Protected Properties and Methods
The protected access modifier ensures that properties or methods are accessible only within the class itself and its derived classes.
Example: Using protected Properties
<?php
class Animal {
protected $type = "Mammal";
protected function getType() {
return $this->type;
}
}
class Dog extends Animal {
public function displayType() {
echo $this->getType();
}
}
$dog = new Dog();
$dog->displayType();
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Mammal
Benefits of Inheritance
- Code Reusability: Reuse code from parent classes, reducing redundancy.
- Extensibility: Add new functionality in child classes without altering parent class code.
- Readability: Organize code logically, making it easier to understand and maintain.
Frequently Asked Questions
Can a child class access private properties of a parent class?
No, private properties and methods are accessible only within the parent class. However, you can use getters or setters in the parent class to allow controlled access.
What happens if a method is defined in both parent and child classes?
The child class’s method overrides the parent class’s method unless the parent’s method is explicitly called using parent::methodName().
Can a PHP class inherit from multiple parent classes?
No, PHP does not support multiple inheritance. However, you can use traits to achieve similar functionality.
Conclusion
Inheritance in PHP is a core concept of object-oriented programming that simplifies code management and promotes reusability. By understanding its syntax, access modifiers, and practical examples, you can efficiently implement inheritance in your PHP projects.
You can also check out our other blogs on Code360.