Using Traits to Reduce Code Duplication in PHP Object‑Oriented Programming
This article explains how PHP traits enable code reuse across multiple classes, illustrating the problem of duplicated methods in Person‑derived classes like Student, Guardian, and Teacher, and showing step‑by‑step refactoring to eliminate redundancy while maintaining clear inheritance structures.
In object‑oriented programming, a trait is a mechanism that allows us to share code among multiple classes. Traits can contain properties, methods, and class constants, helping avoid repetitive code and improving maintainability and readability.
Traits are usually used in the following situations:
Multiple classes share some common properties and methods.
Inheritance alone is not suitable for sharing those properties and methods across several classes.
Let’s look at an example to illustrate why we need and use traits:
<code><?php
class Person
{
public string $name;
public int $age;
public function attendALecture() {}
public function giveALecture() {}
}
class Student extends Person
{
}
class Guardian extends Person
{
}
</code>In the example above we have two classes Student and Guardian that both extend the Person class.
The Student class makes sense because a student should have a name and age and be able to attend and give lectures. However, the Guardian class does not need to attend or give any lectures.
If we do not use a trait, we would have to rewrite the attendALecture() and giveALecture() methods in the Guardian class, resulting in duplicated and hard‑to‑maintain code.
Let’s refactor based on the previous example:
<code><?php
class Person
{
public string $name;
public int $age;
}
class Student extends Person
{
public function attendALecture() {}
public function giveALecture() {}
}
class Guardian extends Person
{
}
</code>In the updated example we can see that the inheritance hierarchy now makes more sense: both Student and Guardian inherit from Person , ensuring they both have the name and age properties.
We then add a new Teacher class, which also needs the lecture methods:
<code><?php
class Person
{
public string $name;
public int $age;
}
class Student extends Person
{
public function attendALecture() {}
public function giveALecture() {}
}
class Guardian extends Person
{
}
class Teacher extends Person
{
public function attendALecture() {}
public function giveALecture() {}
}
</code>Now the Teacher class introduces duplicated attendALecture() and giveALecture() methods, just like the Student class.
We can solve the duplicated‑code problem by using a trait:
<code><?php
trait CanLecture
{
public function attendALecture() {}
public function giveALecture() {}
}
class Person
{
public string $name;
public int $age;
}
class Student extends Person
{
use CanLecture;
}
class Guardian extends Person
{
}
class Teacher extends Person
{
use CanLecture;
}
</code>In this example both Student and Teacher use the CanLecture trait, which provides them with the attendALecture() and giveALecture() methods.
Using a trait prevents us from repeating the same methods in multiple classes, keeping the code clean and maintainable.
Applying traits can be tricky, but there are some guidelines to help decide when to use them.
We can ask ourselves the following questions to determine whether a trait is appropriate:
Do all subclasses need the properties and methods defined in the parent class?
Are there properties or methods that are not needed by every subclass but are used by several of them?
If the answer to both questions is yes, then a trait is a good solution. This approach helps keep inheritance hierarchies clean while reusing functionality across classes.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.