Welcome to this comprehensive guide on using the extends keyword in PHP! In this tutorial, we'll dive deep into inheritance, a fundamental concept in object-oriented programming (OOP). By the end of this lesson, you'll be well-equipped to create powerful, reusable, and maintainable PHP classes. π
Inheritance is a relationship between two classes where a new class, the subclass or derived class, is based on an existing class, the superclass or base class. The subclass inherits properties and methods from the superclass, allowing for code reuse and a more organized structure. π‘
Let's start by creating two simple classes: Animal and Dog. The Animal class will serve as our base class, while Dog will inherit its properties and methods.
// Animal.php
class Animal {
public $name;
public $sound;
function makeSound() {
echo "The {$this->name} makes a sound.";
}
}
// Dog.php
class Dog extends Animal {
public $breed;
function __construct($name, $breed) {
$this->name = $name;
$this->breed = $breed;
}
function makeSound() {
echo "The {$this->name} is a {$this->breed} and its sound is: Bark!";
}
}In the above code, we have defined two classes: Animal and Dog. The Dog class extends Animal, meaning it inherits its properties and methods. We also overrode the makeSound() method in the Dog class to provide a more specific implementation.
Now that we've set up our classes, let's create an instance of the Dog class and call its methods.
$myDog = new Dog("Fido", "Labrador");
$myDog->makeSound();When you run this code, the output will be:
The Fido is a Labrador and its sound is: Bark!
When a subclass inherits from a superclass, it automatically gets access to all the properties and methods of the superclass. Let's demonstrate this with a simple example:
// Vehicle.php
class Vehicle {
public $color;
public $make;
function drive() {
echo "The {$this->color} {$this->make} is driving.";
}
}
// Car.php
class Car extends Vehicle {
function accelerate() {
echo "The Car is accelerating.";
}
}
// Usage
$myCar = new Car();
$myCar->color = "Red";
$myCar->make = "Toyota";
$myCar->drive(); // Output: The Red Toyota is driving.
$myCar->accelerate(); // Output: The Car is accelerating.In this example, the Car class inherits from the Vehicle class, automatically getting access to its properties (color and make) and methods (drive()). We also added a new method, accelerate(), which is exclusive to the Car class.
In PHP, which access modifier is used for properties and methods that are accessible within the class and by inherited classes?
Polymorphism is a powerful feature of OOP that allows objects of different classes to be treated as objects of a common parent class. In PHP, polymorphism is achieved through interfaces and abstract classes.
In this tutorial, we delved into the concept of inheritance in PHP, demonstrating how it allows us to create powerful and maintainable code. By understanding inheritance and its benefits, you're well on your way to becoming a proficient PHP developer!
Remember to explore more about polymorphism and other OOP concepts to take your PHP skills to the next level. Happy coding! π