PHP extends Keyword: Understanding Inheritance in PHP 🎯

beginner
25 min

PHP extends Keyword: Understanding Inheritance in PHP 🎯

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. πŸ“

What is Inheritance in PHP?

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. πŸ’‘

Setting Up Our First Inheritance Example πŸ“

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.

php
// 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.

Using Our Inheritance Example πŸ“

Now that we've set up our classes, let's create an instance of the Dog class and call its methods.

php
$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!

Properties and Methods Inheritance πŸ“

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:

php
// 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.

Quiz: Inheritance and Access Modifiers 🎯

Quick Quiz
Question 1 of 1

In PHP, which access modifier is used for properties and methods that are accessible within the class and by inherited classes?

Polymorphism in PHP πŸ’‘

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.

Conclusion πŸ“

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! πŸš€