本文介绍了PHP Laravel:找不到特质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命名空间和使用上遇到了麻烦.

I have some trouble with namespace and use.

我收到此错误:找不到特质'Billing \ BillingInterface'"

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

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

These are the files in my Laravel application:

Billing.php

Billing.php

namespace Billing\BillingInterface;

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

PaymentController.php

PaymentController.php

use Billing\BillingInterface;

class PaymentsController extends BaseController
{
    use BillingInterface;

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

如何正确使用use和名称空间?

How to i use use and namespace properly?

推荐答案

BillingInterfaceinterface而不是trait.因此无法找到不存在的特征

BillingInterface is an interface not a trait. Thus it can't find the non existent trait

此外,在名为Billing\BillingInterface的命名空间中还有一个名为BillingInterface的接口,该接口的完全限定名称为:\Billing\BillingInterface\BillingInterface

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

也许你是说

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
}

或将其用作特征.

namespace Billing;

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

再次使用修改后的PaymentsController,但具有完全限定的名称.

Again the modified PaymentsController, but with fully qualifies names.

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;
    }
}

这篇关于PHP Laravel:找不到特质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 10:57