Traits in PHP and Laravel

Traits in PHP and LaravelKshitij SoniBlockedUnblockFollowFollowingDec 12, 2017One of the problems of PHP as a programming language is the fact that you can only have single inheritance.

This means a class can only inherit from one other class.

For example, it might be desirable to inherit methods from a couple of different classes in order to prevent code duplication.

In PHP 5.

4 a new feature of the language was added known as Traits and are used extensively in the Laravel Framework.

The exact definition from the PHP site defines Traits as such:“Traits are a mechanism for code reuse in single inheritance languages such as PHP.

A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.

It is not possible to instantiate a Trait on its own.

It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

”What are PHP Traits?A Trait is simply a group of methods that you want include within another class.

A Trait, like an abstract class, cannot be instantiated on it’s own.

An example of a Trait could be:trait SharePost { public function share($item) { return 'share this post'; } }You could then include this Trait within other classes like this:class Post { use SharePost; } class Comment { use SharePost; }Now if you were to create new objects out of these classes you would find that they both have the share() method available:$post = new Post;echo $post->share(''); // 'share this post' $comment = new Comment;echo $comment->share(''); // 'share this post'How do Traits work?As you can see from the example above, both the Post and the Comment objects have the share() method available despite not having that method defined.

A Trait is basically just a way to “copy and paste” code during run time.

This means the Trait is copied in to the Post and Comment classes so when you instantiate a new instance, the share() method code will be available.

I regularly review my code and how I architect my code so that future feature additions can be made quickly and new projects can easily expand on previous ideas.

Now how to use traits in laravel?I have Create a Traits directory in my Http directory with a Trait called BrandsTrait.

phpand use it like:use AppHttpTraitsBrandsTrait;class YourController extends Controller { use BrandsTrait; public function addProduct() { $brands = $this->brandsAll(); }}Here is my BrandsTrait.

php<?phpnamespace AppHttpTraits;use AppBrand;trait BrandsTrait { public function brandsAll() { // Get all the brands from the Brands Table.

$brands = Brand::all(); return $brands; }}Even better, if you find another common method in your models that relates to how the products and brand should be interacted with, the trait might not be a bad place to keep it.

Subscribe to my channel : https://www.



. More details

Leave a Reply