Interfaces and traits

Interfaces and traits share behavior without a family tree. In PHP an interface states what a class must do, a trait hands it a ready method body, and a parent class does both for one family.

Define an interface

An interface lists method names without bodies. It describes what an object can do and hides how it does it.

Keep interfaces small so classes can adopt them without extra weight.

<?php interface HasName { public function name(): string; } class Person implements HasName { public function name(): string { return "Ada"; } } $person = new Person(); echo $person->name(), PHP_EOL;

php interface.php

Ada

The interface holds the signature and the class holds the body. Code that asks only for a HasName can call name() without knowing which class answers.

Implement it

A class joins the contract with the implements keyword. It must then define every listed method.

Different classes can implement the same interface in completely different ways.

<?php interface HasName { public function name(): string; } class Person implements HasName { public function __construct(public string $nick = "Ada") { } public function name(): string { return $this->nick; } } class Robot implements HasName { public function name(): string { return "R2-D2"; } } echo (new Person())->name(), PHP_EOL; echo (new Robot())->name(), PHP_EOL;

php implements.php

Ada
R2-D2

One contract, two bodies: the person reads a property and the robot returns a fixed string. A class that declares implements and leaves a method out is a fatal error until the method exists.

Reuse with traits

A trait holds ready methods for reuse across unrelated classes. Add it with the use keyword inside a class.

More helpers are shown in the guide on traits with string and slug examples.

<?php trait Sluggable { public function slug(string $value): string { return strtolower(trim($value)); } } class Article { use Sluggable; } echo (new Article())->slug(" Hello World "), PHP_EOL;

php trait.php

hello world

The trait adds slug() to Article although the two have no parent in common, and its body leans on trim() for the spaces and strtolower() for the letters. A trait is copied into the class, so $this inside it points at the object of the class that uses it.

Interface vs trait vs parent

Use a parent when classes share one family tree. Use an interface when many different classes promise the same behavior.

Use a trait when the same code body must appear in places with no shared parent.

<?php interface HasName { public function name(): string; } trait Sluggable { public function slug(string $value): string { return strtolower(trim($value)); } } class Content { public function label(): string { return "content"; } } class Article extends Content implements HasName { use Sluggable; public function name(): string { return "Hello World"; } } $article = new Article(); echo $article->label(), PHP_EOL; echo $article->name(), PHP_EOL; echo $article->slug($article->name()), PHP_EOL;

php when.php

content
Hello World
hello world

The object carries all three: the label comes from the parent, the name fills the interface contract, and the slug comes from the trait. Each mechanism answers a different question, which is why one class can use all of them at once.

Exercises

  1. Add a second method to HasName, make every implementing class define it, and run both programs.
  2. Write an Email class that implements HasName and returns its address.
  3. Move slug() from the trait into a class and give the trait to a second, unrelated class. Which version do you prefer?
  4. Remove implements HasName from Article in the last program. Does the output change?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is an interface?

    Answer

    A list of method signatures that a class promises to implement

    The interface names the methods and their types; the class that implements it writes the bodies.

  2. Which keyword makes a class adopt an interface?

    Answer

    implements

    A class extends a parent and implements an interface; use brings a trait in.

  3. What does a trait give a class?

    Answer

    Ready method bodies it can reuse

    The methods of the trait are copied into the class, so unrelated classes can share the same body.

  4. How many interfaces can one class implement?

    Answer

    Several, separated by commas

    PHP has single inheritance for classes but a class may implement any number of interfaces.

  5. What does this program print? <?php interface HasName { public function name(): string; } class Person implements HasName { public function name(): string { return "Ada"; } } echo (new Person())->name();

    Answer

    Ada

    The class fills the contract with its own body, and that body returns the string.

  6. What does this program print? <?php interface HasName { public function name(): string; } class Person implements HasName { public function name(): string { return "Ada"; } } class Robot implements HasName { public function name(): string { return "R2-D2"; } } echo (new Person())->name(), " / ", (new Robot())->name();

    Answer

    Ada / R2-D2

    Both classes implement the same interface with different bodies, so each object answers with its own name.

  7. What does this program print? <?php trait Sluggable { public function slug(string $value): string { return strtolower(trim($value)); } } class Article { use Sluggable; } echo (new Article())->slug(" Hello World ");

    Answer

    hello world

    trim removes the spaces at both ends and strtolower lowers the two words.

  8. What does this program print? <?php trait Sluggable { public function slug(string $value): string { return strtolower(trim($value)); } } class Article { use Sluggable; public function title(): string { return "My Post"; } } $article = new Article(); echo $article->slug($article->title());

    Answer

    my post

    The trait only trims and lowers the text, so the space between the two words stays.

  9. What does this program print? <?php interface HasName { public function name(): string; } class Person implements HasName { public function name(): string { return "Ada"; } } $person = new Person(); var_dump($person instanceof HasName);

    Answer

    bool(true)

    An object of a class that implements an interface is an instance of that interface too.

  10. A class declares implements HasName but writes no name() method. What does PHP do?

    Answer

    It fails with a fatal error until the method exists

    The interface is a contract: a concrete class that leaves a method out cannot be loaded.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev