extends / implements / abstract / interface Since: PHP 5(2004)
Syntax for inheriting classes, implementing interfaces, and defining abstract classes. It improves code reusability and extensibility.
Syntax
// Inherit from a parent class.
class ChildClass extends ParentClass { }
// Define an interface.
interface InterfaceName {
public function method(): void;
}
// Implement an interface.
class ClassName implements InterfaceName { }
// Define an abstract class.
abstract class AbstractClassName {
abstract public function method(): void;
}
Syntax Overview
| Syntax | Description |
|---|---|
| extends | Defines a child class that inherits from a parent class. PHP supports single inheritance only — a class can inherit from only one parent. |
| implements | Implements one or more interfaces. Multiple interfaces can be implemented at once, separated by commas. |
| interface | Defines a type that specifies only method signatures without implementation. It enforces which methods a class must implement. |
| abstract | Defines an abstract class or abstract method. Abstract classes cannot be instantiated, and child classes must implement all abstract methods. |
| parent:: | Calls a method or constructor from the parent class. Use this when you want to run the parent's logic in addition to the overridden behavior. |
Sample Code
<?php
// Basic inheritance example.
class Animal {
public function __construct(
protected string $name
) {}
public function speak(): string {
return "{$this->name} is making a sound.";
}
}
class Dog extends Animal {
public function speak(): string {
return "{$this->name}: Woof!"; // Override the parent method.
}
public function fetch(): string {
return "{$this->name} fetched the ball.";
}
}
$dog = new Dog("Rex");
echo $dog->speak(); // Outputs "Rex: Woof!"
echo $dog->fetch(); // Child class methods are also available.
// Calling the parent constructor with parent::.
class Admin extends User {
public function __construct(
string $name,
int $age,
private string $role
) {
parent::__construct($name, $age); // Run the parent constructor.
}
}
// Defining and implementing interfaces.
interface Loggable {
public function to_log(): string;
}
interface Cacheable {
public function cache_key(): string;
}
// A class can implement multiple interfaces at once.
class Article implements Loggable, Cacheable {
public function __construct(
private int $id,
private string $title
) {}
public function to_log(): string {
return "[Article:{$this->id}] {$this->title}";
}
public function cache_key(): string {
return "article_{$this->id}";
}
}
$article = new Article(1, "PHP Basics");
echo $article->to_log(); // Outputs "[Article:1] PHP Basics"
echo $article->cache_key(); // Outputs "article_1"
// Abstract class example.
abstract class Shape {
abstract public function area(): float;
public function describe(): string {
return "The area is " . $this->area() . "."; // Can call the abstract method.
}
}
class Circle extends Shape {
public function __construct(
private float $radius
) {}
public function area(): float {
return pi() * $this->radius ** 2; // Implement the abstract method.
}
}
class Rectangle extends Shape {
public function __construct(
private float $width,
private float $height
) {}
public function area(): float {
return $this->width * $this->height;
}
}
$circle = new Circle(5);
echo $circle->describe(); // Outputs "The area is 78.539816339745."
$rect = new Rectangle(4, 6);
echo $rect->area(); // Outputs "24"
Notes
Inheritance via extends lets you build on an existing class by adding or modifying functionality. PHP supports single inheritance only — a class can have at most one parent class. To combine behavior from multiple sources, use interfaces or 'trait'.
An interface defines a contract: "you must implement these methods." Because it guarantees the existence of methods without depending on a specific implementation, it helps improve type safety in large applications. An abstract class sits between an interface and a concrete class — it can enforce the implementation of some methods while providing shared logic in others.
For the basics of classes, see 'class'. For access modifiers, see 'public / private / protected'.
If you find any errors or copyright issues, please contact us.