PHP Laravel:未找到特征

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27625707/
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 10:35:06  来源:igfitidea点击:

PHP Laravel: Trait not found

phplaravel

提问by andershagbard

I have some trouble with namespace and use.

我在命名空间和使用方面遇到了一些麻烦。

I get this error: "Trait 'Billing\BillingInterface' not found"

我收到此错误:“未找到特征 'Billing\BillingInterface'”

These are the files in my Laravel application:

这些是我的 Laravel 应用程序中的文件:

Billing.php

帐单.php

namespace Billing\BillingInterface;

interface BillingInterface
{
    public function charge($data);
    public function subscribe($data);
    public function cancel($data);
    public function resume($data);
}

PaymentController.php

支付控制器.php

use Billing\BillingInterface;

class PaymentsController extends BaseController
{
    use BillingInterface;

    public function __construct(BillingPlatform $BillingProvider)
    {
        $this->BillingProvider = $BillingProvider;
    }
}

How to i use use and namespace properly?

如何正确使用 use 和 namespace?

回答by robbmj

BillingInterfaceis an interfacenot a trait. Thus it can't find the non existent trait

BillingInterfaceinterface不是trait。因此它找不到不存在的特征

Also you have an interface called BillingInterfacein a namespace called Billing\BillingInterface, the fully qualified name of the interface is: \Billing\BillingInterface\BillingInterface

此外,您在名为BillingInterface的命名空间中调用了一个接口Billing\BillingInterface,该接口的完全限定名称是:\Billing\BillingInterface\BillingInterface

Perhaps you mean

也许你的意思是

use Billing\BillingInterface\BillingInterface;
// I am not sure what namespace BillingPlatform is in, 
// just assuming it's in Billing.
use Billing\BillingPlatform;

class PaymentsController extends BaseController implements BillingInterface
{
    public function __construct(BillingPlatform $BillingProvider)
    {
        $this->BillingProvider = $BillingProvider;
    }

    // Implement BillingInterface methods
}

Or to use it as a trait.

或者将其用作特征。

namespace Billing;

trait BillingTrait
{
    public function charge($data) { /* ... */ }
    public function subscribe($data) { /* ... */ }
    public function cancel($data) { /* ... */ }
    public function resume($data) { /* ... */ }
}

Again the modified PaymentsController, but with fully qualifies names.

再次修改PaymentsController,但具有完全限定的名称。

class PaymentsController extends BaseController
{
    // use the fully qualified name
    use \Billing\BillingTrait;

    // I am not sure what namespace BillingPlatform is in, 
    // just assuming it's in billing.
    public function __construct(
        \Billing\BillingPlatform $BillingProvider
    ) {
        $this->BillingProvider = $BillingProvider;
    }
}