2

I have some trouble with namespace and use.

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

These are the files in my Laravel application:

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

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?

1 Answer 1

5

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

Also you have an interface called BillingInterface in a namespace called Billing\BillingInterface, the fully qualified name of the interface is: \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.

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;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I dont want to implement to the controller.
@andershagbard Then you will have to make a trait that implements the methods of BillingInterface. However traits are not types in PHP nor can they implement an iterface so you will lose type hierarchy as in PaymentsController is not a BillingInterface. Alternativly and probably the best approach would be to inject the controller with an object that can do billing. Perhaps that is the point of the BillingPlatform but I can't be sure.
Isn't that what I'm doing in my construct function?
@andershagbard If that is what a BillingPlatform is then yes. See the updated answer that includes a trait implementation. Also I have fully qualified all the namespaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.