Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class to inherit properties and methods from another class. This promotes code reusability and establishes a natural hierarchy between classes.
Key Concepts
- Parent Class (Base Class or Superclass): The class whose properties and methods are inherited.
- Child Class (Derived Class or Subclass): The class that inherits properties and methods from the parent class.
extends
Keyword: Used to create a child class that inherits from a parent class.
Example
Let's start with a simple example to illustrate inheritance in PHP.
Parent Class
<?php class Animal { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function describe() { return "This is a $this->color $this->name."; } } ?>
Child Class
<?php class Dog extends Animal { public function bark() { return "Woof! Woof!"; } } $dog = new Dog("Dog", "brown"); echo $dog->describe(); // Output: This is a brown Dog. echo $dog->bark(); // Output: Woof! Woof! ?>
Explanation
- Parent Class (
Animal
): Defines propertiesname
andcolor
, and a methoddescribe()
. - Child Class (
Dog
): Inherits properties and methods fromAnimal
and adds a new methodbark()
.
Practical Exercise
Task
- Create a parent class
Vehicle
with propertiesmake
andmodel
, and a methodgetDetails()
. - Create a child class
Car
that extendsVehicle
and adds a methoddrive()
. - Instantiate the
Car
class and call bothgetDetails()
anddrive()
methods.
Solution
<?php class Vehicle { public $make; public $model; public function __construct($make, $model) { $this->make = $make; $this->model = $model; } public function getDetails() { return "Make: $this->make, Model: $this->model"; } } class Car extends Vehicle { public function drive() { return "The car is driving."; } } $car = new Car("Toyota", "Corolla"); echo $car->getDetails(); // Output: Make: Toyota, Model: Corolla echo $car->drive(); // Output: The car is driving. ?>
Explanation
- Parent Class (
Vehicle
): Defines propertiesmake
andmodel
, and a methodgetDetails()
. - Child Class (
Car
): Inherits properties and methods fromVehicle
and adds a new methoddrive()
.
Common Mistakes
-
Forgetting to Call the Parent Constructor: If the parent class has a constructor, you should call it from the child class constructor using
parent::__construct()
.class Car extends Vehicle { public function __construct($make, $model) { parent::__construct($make, $model); } public function drive() { return "The car is driving."; } }
-
Overriding Methods Incorrectly: When overriding a method in the child class, ensure the method signature matches the parent class method.
Summary
- Inheritance allows a class to inherit properties and methods from another class, promoting code reusability.
- Use the
extends
keyword to create a child class that inherits from a parent class. - Always call the parent constructor if the parent class has one.
- Be cautious when overriding methods to ensure the method signatures match.
In the next topic, we will explore Interfaces and Abstract Classes, which provide additional ways to define and enforce the structure of your classes.
PHP Programming Course
Module 1: Introduction to PHP
- What is PHP?
- Setting Up the Development Environment
- Your First PHP Script
- PHP Syntax and Variables
- Data Types in PHP
Module 2: Control Structures
Module 3: Functions
- Defining and Calling Functions
- Function Parameters and Return Values
- Variable Scope
- Anonymous Functions and Closures
Module 4: Arrays
Module 5: Working with Forms
Module 6: Working with Files
Module 7: Object-Oriented Programming (OOP)
- Introduction to OOP
- Classes and Objects
- Properties and Methods
- Inheritance
- Interfaces and Abstract Classes
- Traits
Module 8: Working with Databases
- Introduction to Databases
- Connecting to a MySQL Database
- Performing CRUD Operations
- Using PDO for Database Interaction
- Database Security
Module 9: Advanced PHP Techniques
- Error and Exception Handling
- Sessions and Cookies
- Regular Expressions
- Working with JSON and XML
- PHP and Web Services
Module 10: PHP Frameworks and Best Practices
- Introduction to PHP Frameworks
- Getting Started with Laravel
- MVC Architecture
- Best Practices in PHP Development
- Testing and Debugging