Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. extends / implements / abstract / interface

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

SyntaxDescription
extendsDefines a child class that inherits from a parent class. PHP supports single inheritance only — a class can inherit from only one parent.
implementsImplements one or more interfaces. Multiple interfaces can be implemented at once, separated by commas.
interfaceDefines a type that specifies only method signatures without implementation. It enforces which methods a class must implement.
abstractDefines 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

sample_extends.php
<?php
// Basic inheritance example.
class Fighter {
	public function __construct(
		protected string $name
	) {}

	public function battle(): string {
		return "{$this->name} is bracing for it.";
	}
}

class KofFighter extends Fighter {
	public function battle(): string {
		return "{$this->name} moves in!"; // Override the parent method.
	}

	public function special(): string {
		return "{$this->name}: Oni Yaki!";
	}
}

$fighter = new KofFighter("Yagami Iori");
echo $fighter->battle(); // Outputs "Yagami Iori moves in!"
echo $fighter->special(); // Child class methods are also available.

// Calling the parent constructor with parent::.
class User {
	public function __construct(
		protected string $name,
		protected int $age
	) {}
}

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"
php sample_extends.php
Yagami Iori moves in!Yagami Iori: Oni Yaki![Article:1] PHP Basicsarticle_1The area is 78.539816339745.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 .