PHP Reference ยท Functions and Classes
PHP inheritance: extends, parent and final
Reference for PHP inheritance: extends, overriding methods, parent::, the final keyword, constructor behavior, signature compatibility rules and #[Override].
Inheritance lets a class extend another class and reuse its properties, methods and constants. The subclass (child) gets everything non-private from the parent and can add members or override methods. PHP supports single inheritance: a class has at most one parent.
Inside a child, parent::method() calls the parent's version. The final keyword stops a class from being extended or a method from being overridden.
class Animal { public function speak(): string { return "..."; } } class Dog extends Animal { public function speak(): string { return "Woof"; } } echo (new Dog)->speak(); // Woof
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
[final | abstract] class Child extends Parent { } class Child extends Parent { final public function locked(): void {} #[\Override] public function replaced(): void { parent::replaced(); } }
Override rules (Liskov compatibility):
| Member | Rule |
|---|---|
| Parameters | Types may be widened (contravariant); you may add optional parameters. |
| Return type | May be narrowed (covariant), for example Animal to Dog. |
| Properties | Type must match exactly (readonly status too). |
| Visibility | May be widened (protected to public), never narrowed. |
| Constructors | Exempt from signature compatibility, unless declared in an interface or abstract. |
Errors: extending a final class, overriding a final method or constant, or an incompatible signature is a fatal error at compile time. #[Override] (PHP 8.3) triggers a fatal error if the method does not actually override anything.
Usage notes
- A child constructor replaces the parent's. Call
parent::__construct()yourself if the parent needs initialization. - Private members are invisible to children. Use
protectedto share with subclasses. parent::,self::andstatic::pick the parent class, the class where the code is written, and the called class respectively.- Prefer composition when the relationship is "has a" rather than "is a". Deep hierarchies are brittle.
- Interfaces and traits cover what single inheritance cannot: see Interfaces and Traits.
- Type checks:
instanceof,is_a(),is_subclass_of(),get_parent_class()andclass_parents()inspect the chain. - Static members are inherited; a static property is shared between parent and child unless redeclared.
- Final classes are easier to reason about; make classes final by default unless you design them for extension.
- Abstract classes can leave methods unimplemented: see Abstract classes.