如何在 Laravel 5.4.18 中使用特性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43433350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:47:53  来源:igfitidea点击:

How to use traits in Laravel 5.4.18?

phplaraveltraits

提问by cfrostte

I need an example of where to exactly create the file, to write to it, and how to use the functions declared in the trait. I use Laravel Framework 5.4.18

我需要一个示例,说明在何处准确创建文件、写入文件以及如何使用特征中声明的函数。我使用 Laravel 框架 5.4.18

-I have not altered any folder in the framework, everything is where it corresponds-

-我没有改变框架中的任何文件夹,一切都在它对应的地方-

From already thank you very much.

从已经非常感谢你。

回答by Mayank Pandeyz

I have Create a Traits directory in my Httpdirectory with a Trait called BrandsTrait.php

我在我的Http目录中创建了一个 Traits 目录,其中一个 Trait 叫做BrandsTrait.php

and use it like:

并像这样使用它:

use App\Http\Traits\BrandsTrait;

class YourController extends Controller {

    use BrandsTrait;

    public function addProduct() {

        //$brands = Brand::all();

        // $brands = $this->BrandsTrait();  // this is wrong
        $brands = $this->brandsAll();
    }
}

Here is my BrandsTrait.php

这是我的 BrandsTrait.php

<?php
namespace App\Http\Traits;

use App\Brand;

trait BrandsTrait {
    public function brandsAll() {
        // Get all the brands from the Brands Table.
        $brands = Brand::all();

        return $brands;
    }
}

Note:Just like a normal function written in a certain namespace, you can use traitsas well

注:就像写在一个特定的正常功能namespace,您可以使用traits,以及

回答by Roham Rafii

Trait description:

特性说明:

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.

Traits 是一种在单继承语言(如 PHP)中代码重用的机制。Trait 旨在通过使开发人员能够在位于不同类层次结构中的多个独立类中自由重用方法集来减少单继承的一些限制。Traits 和类组合的语义以一种降低复杂性并避免与多重继承和 Mixin 相关的典型问题的方式定义。

The solution

解决方案

Make a directory in your App, named Traits

在您的应用程序中创建一个目录,命名为 Traits

Create your own trait in Traitsdirectory ( file: Sample.php ):

Traits目录(文件:Sample.php)中创建您自己的特征:

<?php

namespace App\Traits;

trait Sample
{
    function testMethod()
    {
        echo 'test method';
    }
}

Then use it in your own controller:

然后在你自己的控制器中使用它:

<?php
namespace App\Http\Controllers;

use App\Traits\Sample;

class MyController {
    use Sample;
}

Now the MyControllerclass has the testMethodmethod inside.

现在这个MyControllertestMethod里面有方法。

You can change the behavior of trait methods by overriding them it in MyControllerclass:

您可以通过在MyController类中覆盖它们来更改 trait 方法的行为:

<?php
namespace App\Http\Controllers;

use App\Traits\Sample;

class MyController {
    use Sample;

    function testMethod()
    {
        echo 'new test method';
    }
}

回答by Plabon Dutta

Let's look at an example trait:

让我们看一个示例特征:

namespace App\Traits;

trait SampleTrait
{
    public function addTwoNumbers($a,$b)
    {
        $c=$a+$b;
        echo $c;
        dd($this)
    }
}

Then in another class, just import the trait and use the function with thisas if that function was in the local scope of that class:

然后在另一个类中,只需导入特征并使用该函数,this就好像该函数在该类的本地范围内一样:

<?php

namespace App\ExampleCode;

use App\Traits\SampleTrait;

class JustAClass
{
    use SampleTrait;
    public function __construct()
    {
        $this->addTwoNumbers(5,10);
    }
}