The following code shows how to use inheritance during class definition.
<?php class Person { protected $firstname; protected $surname; public function __construct(string $firstname, string $surname) { $this->firstname = $firstname; $this->surname = $surname; } //w ww . ja v a 2 s .com public function getFirstname(): string { return $this->firstname; } public function getSurname(): string { return $this->surname; } } class Customer extends Person { private static $lastId = 0; private $id; private $email; public function __construct( int $id, string $name, string $surname, string $email ) { if (empty($id)) { $this->id = ++self::$lastId; } else { $this->id = $id; if ($id > self::$lastId) { self::$lastId = $id; } } $this->name = $name; $this->surname = $surname; $this->email = $email; } public static function getLastId(): int { return self::$lastId; } public function getId(): int { return $this->id; } public function getEmail(): string { return $this->email; } public function setEmail($email): string { $this->email = $email; } } ?>
The keyword extends tells PHP that this class is a child of the Person class.
To reference the parent's implementation, use the keyword parent:: instead of $this.
public function __construct( int $id, string $firstname, string $surname, string $email ) { parent::__construct($firstname, $surname); if (empty($id)) { $this->id = ++self::$lastId; } else { $this->id = $id; if ($id > self::$lastId) { self::$lastId = $id; } } $this->email = $email; }