我在命名空间和使用方面遇到了一些问题。
我收到此错误:“未找到特征‘Billing\BillingInterface’”
这些是我的 Laravel 应用程序中的文件:
计费.php
namespace Billing\BillingInterface;
interface BillingInterface
{
public function charge($data);
public function subscribe($data);
public function cancel($data);
public function resume($data);
}
支付 Controller .php
use Billing\BillingInterface;
class PaymentsController extends BaseController
{
use BillingInterface;
public function __construct(BillingPlatform $BillingProvider)
{
$this->BillingProvider = $BillingProvider;
}
}
如何正确使用use和命名空间?
最佳答案
BillingInterface 是一个 interface 而不是 trait。因此它找不到不存在的特征
在名为 Billing\BillingInterface 的命名空间中还有一个名为 BillingInterface 的接口(interface),该接口(interface)的完全限定名称为:\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,但具有完全限定名称。
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 : Trait not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625707/