The PHP "extends" Keyword: A Comprehensive Guide

The "extends" keyword is a feature of object-oriented programming in PHP that allows a child class to inherit properties and methods from a parent class. In this article, we will explore the syntax and usage of the "extends" keyword in depth, and provide plenty of examples to help you master this important PHP feature.

Syntax

The "extends" keyword is used to create a child class that inherits properties and methods from a parent class. Here is the basic syntax for using the "extends" keyword in PHP:

class ChildClass extends ParentClass {
  // code to be executed
}

In this example, the "extends" keyword is used to create a child class that inherits properties and methods from a parent class.

Examples

Let's look at some practical examples of how the "extends" keyword can be used:

<?php

// Example 1
class Fruit {
  public $name;
  public $color;
  
  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  
  function getInfo() {
    echo "This fruit is a " . $this->name . " and it is " . $this->color . ".";
  }
}

class Apple extends Fruit {
  function getInfo() {
    echo "This fruit is an " . $this->name . " and it is " . $this->color . ".";
  }
}

$apple = new Apple("apple", "red");
$apple->getInfo();

// Output: This fruit is an apple and it is red.

// Example 2
class Car {
  public $model;
  public $year;
  
  function __construct($model, $year) {
    $this->model = $model;
    $this->year = $year;
  }
  
  function getInfo() {
    echo "This car is a " . $this->model . " and it was made in " . $this->year . ".";
  }
}

class Toyota extends Car {
  function __construct($year) {
    parent::__construct("Toyota", $year);
  }
}

$toyota = new Toyota(2021);
$toyota->getInfo();

// Output: This car is a Toyota and it was made in 2021.

In these examples, we use the "extends" keyword to create child classes that inherit properties and methods from parent classes.

Benefits

Using the "extends" keyword has several benefits, including:

  • Code reusability: The "extends" keyword allows you to reuse code by creating child classes that inherit properties and methods from parent classes.
  • Simplified code: The "extends" keyword can help you simplify your code by reducing the amount of code that needs to be written.

Conclusion

In conclusion, the "extends" keyword is a powerful tool for PHP developers who are using object-oriented programming. It allows you to create child classes that inherit properties and methods from parent classes, making your code more efficient and easier to maintain. We hope this comprehensive guide has been helpful, and we wish you the best of luck as you continue to develop your PHP skills.

Practice Your Knowledge

What is the significance of the 'extends' keyword in PHP?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?