This post is from the professional Laravel developer community, original link: learnku.com/laravel/t/3…

In fact, one of the problems with PHP as a programming language is that you can only use single inheritance. This means that a class can only inherit from another class. For example, you might want to inherit methods from several different classes to prevent code duplication. A new language feature was added in PHP 5.4, known as Traits, which is widely used in the Laravel framework.

The official PHP website defines Traits as follows:

“Traits are a mechanism for code reuse in single-inheritance languages such as PHP. Trai is designed to remove some of the limitations of single-inheritance languages, allowing developers the freedom to reuse methods in several separate classes in different class hierarchies. The semantic combination of traits and class definition reduces code complexity to some extent, avoiding some of the typical problems associated with multiple inheritance and Mixins.

Traits are very similar to classes, but their purpose is simply to bring together methods in a better, consistent way. Traits themselves cannot be instantiated. It can combine behavior horizontally in addition to traditional inheritance, in other words, class applications do not need inheritance.”

What are PHP Traits?

A Trait is a simple set of methods contained in another class. A Trait, like an abstract class, cannot be instantiated independently.

An example Trait might look like this:

trait SharePost {

  public function share($item)
  {
    return 'share this post'; }}Copy the code

You can then use traits like this in other classes:

class Post {

  use SharePost;

}

class Comment {

  use SharePost;

}
Copy the code

Now, if you want to create new objects from these classes, you’ll find that they can all be created using the share() method:

$post = new Post;
echo $post->share(' '); // 'share this post' 

$comment = new Comment;
echo $comment->share(' '); // 'share this post'
Copy the code

How do Traits work?

As you can see from the above example, Post and Comment can call the share() method even though they don’t define methods themselves.

Traits are a way to “copy and paste” code at runtime.

This means that traits are copied to Post and Comment, so when you instantiate a new instance, you can call the share() method directly.

I regularly review my code and how to construct new code so that I can quickly build new features in the future and easily start new projects.

So, how do you use traits in laravel?

First, I created a Traits folder in the Http directory of my project and created a Trait file called BrandStrait.php

Use it like this:

use App\Http\Traits\BrandsTrait;

class YourController extends Controller {
		use BrandsTrait;
		
		public function addProduct() {
			$brands = $this->brandsAll(); }}Copy the code

This is my BrandsTrait. PHP

<? php namespace App\Http\Traits; use App\Brand; trait BrandsTrait { publicfunction brandsAll() {// Get all brands from the brand table.$brands = Brand::all();
		
		return $brands; }}Copy the code

Even more, if you find another general way in the model that relates to product and brand interaction, it’s a good choice to code in that trait.