Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PHP best practices for using Traits and Interfaces

Thursday, August 10th, 2017 by Servage

php-codeDo you often notice yourself ending up writing the same code in multiple files? Do you wish you could force similar classes to follow the same structure? Traits and interfaces are something new PHP developers tend to overlook, but they are perfect in solving problems like these.

Traits: An Alternative to Multiple Inheritance

Traits have been available in PHP since 5.4. A trait is a PHP file that uses the trait keyword instead of class. Traits and classes are quite similar: classes can be extended and traits can be used inside other classes to prevent having to write same code twice in different files. Instead of writing the same method for two classes, you can create it in a trait and make the two classes use that trait.

One of the benefits of traits over classes and inheritance is the ability to use multiple traits inside one class. This is sometimes compared to multiple inheritance which is a feature PHP does not support.

Here is a real-world example of using traits: You have an Author and Admin class that extend a base class called User. The Author class should be deletable but Admin should not. Because of this, you can’t add a delete() method to the base class. Instead, you can create a Deletable trait and make the Author class use it. The Deletable trait contains the logic to delete users.

Interfaces: Common Rules for Classes

Interfaces are somewhat similar to traits. However, there is one major difference between them: interfaces only define the behavior of classes. They only contain the signature of functions. A function signature means the function name and its parameters. Interfaces don’t contain any code in the function body since doing so is up to the class that implements it. An example of a signature looks like this: public function changeName(string $name). As you can see, the body of the function is missing since the function is defined inside an interface.

Interfaces can be used to make multiple related classes to work in a similar fashion. For example, if we consider the above two classes (Author and Admin), they both can have a changeName() method. Defining the method signature in an interface forces a developer to implement the method in a standardized way for both of these classes. For example, the name must be changeName(). It can’t be called changeName() for Author and updateName() for Admin.

This is one of the primary benefits of interfaces: they help us keep method names and the arguments we can pass to them consistent between classes. The other benefit is that classes implementing an interface must contain the functionality defined in the interface. In this case, it must be possible to change the name of all users.

PHP best practices for using Traits and Interfaces, 4.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.