Inheritance
Last updated onEver felt like you’re rewriting the same logic over and over in your PHP classes? Maybe it’s the same properties or methods that just keep popping up. Here's where PHP inheritance steps in to avoid that.
With inheritance, you can set up shared behaviors in one place—so your code is more streamlined, less repetitive, and, most importantly, easier to manage.
In the next sections, you will learn how PHP inheritance can help you write cleaner, simpler code without all the repetition.
What is PHP Inheritance?
Generally, inheritance is a way for one class to gain the properties and methods of another. So if you have a class called
with properties like Vehicle
and $wheels
. Instead of creating a $engine
class and retyping those same properties, you can set up Car
as a parent class. Then, Vehicle
can inherit all that useful info directly from Car
without duplicating code. Vehicle
This lets you define shared traits in one place, saving you from repeating yourself and keeping things organized.
Inheritance creates a structure where the parent class holds common traits, while child classes use and expand upon those traits as needed.
In the following example, you will learn how to write inheritance in PHP:
class ParentClass {
public $property;
public function method() {
// Code here
}
}
class ChildClass extends ParentClass {
// Additional properties or methods specific to ChildClass
}
In this setup:
ParentClass
defines common properties and methods.ChildClass
inherits from ParentClass and gets access to all public and protected methods and properties.
The “extends” keyword is the key to inheritance, telling PHP that
should inherit everything in ChildClass
. ParentClass
Anyway, In the below section, you’ll see how to set up inheritance in PHP.
Setting Up Inheritance in PHP
Consider you are creating a system that manages different kinds of vehicles. You might start with a
class:Vehicle
class Vehicle {
protected $wheels;
protected $engine;
public function __construct($wheels, $engine) {
$this->wheels = $wheels;
$this->engine = $engine;
}
public function startEngine() {
return "Engine started";
}
}
Now, if you want to create specific vehicle types like
or Car
, these classes can be inherited from Bike
:Vehicle
class Car extends Vehicle {
public function drive() {
return "Car is driving with " . $this->wheels . " wheels.";
}
}
class Bike extends Vehicle {
public function pedal() {
return "Bike is pedaling with " . $this->wheels . " wheels.";
}
}
So,
and Car
inherit the properties Bike
and $wheels
, as well as the $engine
method from startEngine()
. This structure is tidy, prevents repetitive code, and allows each class to add its unique traits. Vehicle
You are now seeing how inheritance can simplify your code and improve the code readability. In the following section, you will learn the types of inheritance in PHP and which one you have to use.
Types of PHP Inheritance and Which One Do You Need?
PHP primarily supports single inheritance. This means a class can inherit from only one parent class, which can sometimes feel limiting. But don’t worry—PHP offers two ways to work around this if you need more flexibility:
- Single Inheritance: As the name suggests, each class can have only one parent class. So,
Car
can inherit fromVehicle
but not from multiple classes. - Interfaces: If you need a class to follow multiple “rules” without forcing a strict hierarchy, interfaces are doing that. An interface lets you define specific methods a class must implement, allowing a class to take on several behaviors.
- Traits: For times when you want to share methods across classes but don’t want strict inheritance, PHP offers traits. Traits let you add methods to multiple classes without creating a formal parent-child relationship. They’re handy for sharing functionality without locking classes into a hierarchy.
Each of these options provides a way to tailor your code structure based on what you are trying to achieve.
In the next section, let’s bring it all together with some examples.
Example of PHP Inheritance
Here, we can take our
example and expand it to show how inheritance plays together.Vehicle
class Vehicle {
protected $engine;
public function startEngine() {
return "Starting engine...";
}
}
class Car extends Vehicle {
public function honk() {
return "Honk! Honk!";
}
}
class Motorcycle extends Vehicle {
public function revEngine() {
return "Revving engine!";
}
}
In this example, the
and Car
both inherit from Motorcycle
, but each adds something unique—Vehicle
for honk()
and Car
for revEngine()
. This way, you maintain shared functionality in Motorcycle
, while each child class focuses on its specific behavior.Vehicle
Anyway, let's summarize it.
Wrapping Up
PHP inheritance lets you share common properties and methods across different classes, saving you time and simplifying your code. Here is a quick summary of the basics:
- Parent Class: The main class that holds shared traits.
- Child Class: Inherits from the parent class and can add or override methods.
- Single Inheritance: PHP only allows one parent class per child class, but you can use traits and interfaces for flexibility.
- Syntax: Use
extends
to set up inheritance, and define unique methods in child classes.
If you need to read more PHP tutorials, click here. Thank you for reading.
Frequently Asked Questions (FAQs)
What is 'PHP inheritance'?
How do you set up inheritance in PHP?
What are the benefits of inheritance in PHP?
Can a child class in PHP have more than one parent class?
What is single inheritance in PHP?
What are interfaces and traits, and how do they work with inheritance?
What is an example of PHP inheritance with a parent and child class?
How does inheritance help with code readability in PHP?
Can child classes override methods from a parent class in PHP?
How do traits add functionality without inheritance?