PHP

PHP Menu

Once you have written a class, you can derive subclasses from it. This can save lots of painstaking code rewriting: you can take a class similar to the one you need to write, extend it to a subclass, and just modify the parts that are different. This is achieved using the extends operator.

The following example illustrates inheritance in PHP.

<?php
class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function get_age() { return $this->age; } }
// Student is inherited from Person class Student extends Person { public function sayHello() { echo "Hello I am a student."; } }
class Employee extends Person { public function sayHi() { echo "Hello I am an Employee."; } }
$std = new Student('Frank', 17); $std->sayHello(); echo "<br>"; echo "I am " . $std->get_age() . "years old."; echo "<br>"; $worker = new Employee('John', 35); $worker->sayHi(); echo "<br>"; echo "I am " . $worker->get_age() . "years old.";

Inherited methods can be overridden by redefining the method in the child class. Note that the same method name should be used as in the example below:

<?php
class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function sayHello() { echo "Hello I am " . $this->name . " and I am " . $this->age . " years old."; } }
// Student is inherited from Person class Student extends Person { public $name; public $age; public $course; public function __construct($name, $age, $course) { $this->name = $name; $this->age = $age; $this->course = $course; } public function sayHello() { echo "Hello I am " . $this->name . " and I am " . $this->age . " years old. I study " . $this->course; } }
$std = new Student('Frank', 17, 'Biology'); $std->sayHello();

To prevent overriding methods of the parent class, the final keyword is used. the following script will result to an error.

<?php
class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } final public function sayHello() { echo "Hello I am " . $this->name . " and I am " . $this->age . " years old."; } }
// Student is inherited from Person class Student extends Person { public $name; public $age; public $course; public function __construct($name, $age, $course) { $this->name = $name; $this->age = $age; $this->course = $course; } public function sayHello() { echo "Hello I am " . $this->name . " and I am " . $this->age . " years old. I study " . $this->course; } } $std = new Student('Frank', 17, 'Biology'); $std->sayHello();

Exercise

Create a class named Car with a property, name. Within the Car class, create a constructor setting the name. Also create a method, printDetails that prints the following: "Car Name: <name of car>". Create a child class (extending the parent) and name it Ford. Override the methods of the parent class to take another parameter, country, and the method to print: "Car Name: <name of car> - Country: <country>". Create an instance of the child class with name = "Ford" ; country = "USA".

<?php
<?php
class Car { public $name; public function __construct($name) { $this->name = $name; } public function printDetails() { echo "Car Name: " . $this->name; } }
class Ford extends Car { public $name; public $country; public function __construct($name, $country) { $this->name = $name; $this->country = $country; } public function printDetails() { echo "Car Name: " . $this->name . " - Country: " . $this->country; } }
$car = new Ford('Ford','USA'); $car->printDetails();
{ "test_output_contains": { "expected":"Car Name: Ford - Country: USA", "error_message":"Sorry, the output is wrong." }, "test_variable_exists": [ { "object":"$name", "error_message":"Have you declared <code>$name<\/code>?" }, { "object":"$country", "error_message":"Have you declared <code>$country<\/code>?" } ], "test_function_exists": { "object":"printDetails", "error_message":"Did you create <code>printDetails<\/code>?" }, "success_message":"Good job!", "error_message":"There is something wrong on your code." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods