Web programming

Traits in Object-Oriented Programming (OOP)

Ngoc Phuong

Ngoc Phuong

2021-03-09 . 96 view

The PHP language only supports single inheritance: a child class can only inherit from one parent class. To overcome the limitations of single inheritance in reusing source code, from PHP 5.4 onward, PHP supports Traits to help developers reuse methods from different classes more easily.

I. Understanding Traits in PHP

1. What are Traits?

Traits are used to declare methods that can be used in multiple classes.

Traits can be understood as a class that groups a set of methods we want to use in another class. It cannot be instantiated; instead, we use the use keyword.

To declare Traits we use the trait keyword.

trait TraitName {
  // some code...
}

To use Traits in a class, we use the use keyword.

class MyClass {
  use TraitName;
}

2. Characteristics of Traits

  • Traits function to consolidate duplicate code snippets into one place for use in many places, and if changes are needed, you only need to modify a single file.
  • Traits, like an abstract class (both cannot be instantiated), but are not completely the same.
  • Methods within Traits can be overridden in the class using them.

3. Advantages and Disadvantages of Traits

Advantages

  • Reduces code duplication, adhering to the principle (DRY - Don't Repeat Yourself).
  • Overcomes the weakness of PHP's single inheritance.

Disadvantages

  • Using Traits can make it difficult to read methods from a class that uses Traits.

II. Points to Note When Using Traits

1. Trait Takes Precedence Over Extends

For example, if we have a Trait RollUp and a Class Person, both with the method sayHello(), and a class Student extends class Person and uses Trait, the program will prioritize the sayHello() method in Trait.

class Person {
    public function sayHello() {
        echo 'Hello!';
    }
}

trait RollUp {
    public function sayHello() {
        echo 'Hi!';
    }
}

class Student extends Person {
    use RollUp;
}

$student = new Student();
$student->sayHello();

2. Handling Conflicts When Using Multiple Traits with Methods of the Same Name

For example, if we have two Traits, Admin and Member, both with the login() method, to use the login() method from Trait Member in the class Client, we use the insteadof keyword to set priority for the method you want to use.

trait Admin {
    public function login() {
        echo 'Login for Admin';
    }
}

trait Member {
    public function login() {
        echo 'Login for Member';
    }
}

class Client{
    use Admin, Member{
    Member::login insteadof Admin; // Use the login method from the Member trait instead of Admin
    } 
}

$client = new Client();
$client->login();

3. Changing the Access Scope of the Method in Trait

For example, if we have a Trait User with the login() method as public, and we want to change the login() method scope to protected in the class Client using Trait, we can do the following

trait User {
    public function login() {
        echo 'Login Success!';
    }
}

// Change the access scope of the login method
class Client {
    use User { login as protected; }
}

4. Traits Can Also Have Abstract Methods

We can define abstract methods within Traits.

trait Hello
{
    abstract public function sendMessage();

    public function hello()
    {
        echo "Hello World";
    }

} 

class Messenger
{
    use Hello;
    public function sendMessage()
    {
        echo 'I am going to say hello';
        echo $this->hello();
    }
}

$sms = new Messenger;
$sms->sendMessage();

5. Traits Can Be Nested

Nested Traits mean that if Trait A has method a(), and Trait B uses Trait A, then Trait B will also be able to use method a().

trait A
{
   public function a(){
     echo "Hihi";
   }
}
trait B
{
    use A;
}

class C {
  use B;
}

$c = new C();
$c->a();

 

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback