All articles

Build reusable PHP components

Can a PHP Class Extend Two Classes? Use Interfaces, Traits, or Composition

See how one PHP class combines a parent, two interfaces, a trait, and a collaborator without extending a second class.

Editorial illustration for Can a PHP Class Extend Two Classes? Use Interfaces, Traits, or Composition

No. A PHP class can extend only one parent class. If your class already extends a base class, adding another name to its extends clause is not a way to acquire a second parent. Instead, decide what you need from the other class: a required set of methods, a reusable method implementation, or a service your object can call. Those are different relationships, and PHP has a different tool for each.

Keep the parent and add interface contracts

A class can extend a class and implement interfaces in the same declaration. The order matters: extends comes before implements, as shown in the PHP interfaces manual. A class can also implement multiple interfaces. Neither interface becomes another parent class.

An interface states what methods a class must provide without supplying their implementations. That makes it useful when other code needs to rely on a capability, such as rendering or providing an identifier. It does not, by itself, give the class the code for those methods. The class still has to provide them.

Put the four relationships in one Report

This self-contained example uses BaseReport as the one parent. Its protected heading() method is available to Report, which uses it while implementing Renderable. Report also implements Identifiable, takes a method from HasLabel, and receives a separate DeliveryService object for delivery. The trait supplies a method; the collaborator does a job for the report. Neither is a second parent.

<?php

interface Renderable
{
    public function render();
}

interface Identifiable
{
    public function id();
}

class BaseReport
{
    protected function heading()
    {
        return 'Report';
    }
}

trait HasLabel
{
    public function label()
    {
        return 'Monthly report';
    }
}

class DeliveryService
{
    public function deliver($content)
    {
        return 'Delivered: ' . $content;
    }
}

class Report extends BaseReport implements Renderable, Identifiable
{
    use HasLabel;

    private $delivery;

    public function __construct(DeliveryService $delivery)
    {
        $this->delivery = $delivery;
    }

    public function render()
    {
        return $this->heading() . ': ' . $this->label();
    }

    public function id()
    {
        return 'report-1';
    }

    public function send()
    {
        return $this->delivery->deliver($this->render());
    }
}

$report = new Report(new DeliveryService());

echo $report->render() . PHP_EOL;
echo $report->id() . PHP_EOL;
echo $report->label() . PHP_EOL;
echo $report->send() . PHP_EOL;

var_dump($report instanceof Renderable);
var_dump($report instanceof Identifiable);
var_dump($report instanceof BaseReport);
var_dump($report instanceof DeliveryService);

Reading the methods gives the expected first four lines: Report: Monthly report, report-1, Monthly report, and Delivered: Report: Monthly report. The four checks are expected to produce true, true, true, and false, respectively. The report satisfies both interface checks and is a child of BaseReport; holding a DeliveryService does not make it a child of DeliveryService. These are expected results from the example, not a claim that it was executed.

Notice where each piece of work occurs. The inherited protected helper contributes Report to render(). The two interface methods, render() and id(), are implemented in Report. The trait contributes label(), which render() calls. Finally, send() asks the injected collaborator to deliver the rendered text. The PHP inheritance manual distinguishes this protected-method inheritance from private parent methods, which a child cannot access.

Choose the relationship before the syntax

Use the single parent, including an abstract parent when that is the relationship you need, for inheritance from a base class. Ask whether another requirement is a contract callers need to rely on. If so, an interface names the required methods, and the class can implement it alongside other interfaces. Ask whether it is a method implementation to reuse. A trait can supply that behavior without becoming a parent. If the class instead needs another object to perform a task, keep that object as a collaborator and call it. The PHP inheritance overview at Phppot describes both multiple-interface implementation and trait-based reuse, and recommends composition when one class uses another.

These choices can coexist, as they do in Report, but they do not turn single inheritance into multiple inheritance. Before adding each responsibility, identify whether it is a required contract, a reusable method, or a collaborating object. That question is more useful than trying to fit two class names after extends.

← All articles