PHP Reference ยท Functions and Classes
PHP abstract classes and methods
Reference for PHP abstract classes and abstract methods: declaring, extending, rules for implementation, abstract vs interface and common fatal errors.
An abstract class is a class that cannot be instantiated directly. It exists to be extended. It may contain ordinary methods and properties plus abstract methods: declarations with a signature but no body. Every concrete subclass must implement them.
Use an abstract class when subclasses share real code and state, and you also want to force each one to supply a few specific parts (the template method pattern).
abstract class Shape { abstract public function area(): float; public function describe(): string { return static::class . " with area " . $this->area(); } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
abstract class Name { abstract [visibility] function method(params): returnType; // concrete methods, properties and constants are allowed too } class Concrete extends Name { [visibility] function method(params): returnType { /* body */ } }
Rules:
- A class with at least one abstract method must itself be declared
abstract. - Abstract methods have no body (just
;) and cannot beprivateorfinal. - The implementing method must be at least as visible as the abstract declaration and have a compatible signature.
- Abstract classes can have constructors, properties and constants.
- A subclass that does not implement every abstract method must also be declared
abstract. - Abstract static methods are allowed.
Errors: new on an abstract class throws Error ("Cannot instantiate abstract class X"). A concrete class missing an abstract method is a fatal error at compile time.
Usage notes
- Abstract class vs interface. An interface is a pure contract and a class may implement many; an abstract class may share code and state but a class can extend only one.
- You can combine them: an abstract class implements an interface, defines the shared methods and leaves the rest abstract.
- Calling an abstract method from the base class is the core of the template method pattern, as
render()does above. - Abstract properties (PHP 8.4) can be declared in abstract classes and interfaces, using property hooks.
- Traits can declare abstract methods to require the using class to supply them. See Traits.
- Do not overuse. If subclasses share no code, an interface is lighter.
- Reflection:
ReflectionClass::isAbstract()andisInstantiable()tell them apart. - Anonymous classes can extend an abstract class if they implement everything.