bencarter78
bencarter78

Reputation: 3895

Error __construct() must be an instance of Interface, none given

I'm developing a package for Laravel and I'm getting an error which I can't figure out how to fix:

Argument 1 passed to Cartalini\Drayman\Drayman::__construct() must be an instance of Cartalini\Drayman\Repositories\UserRepositoryInterface, none given, called in /Applications/MAMP/htdocs/l4/app/controllers/HomeController.php on line 10 and defined

Here's my code...

namespace Cartalini\Drayman;

use Cartalini\Drayman\Repositories\UserRepositoryInterface;

class Drayman
{
    protected $user;

    public function __construct(UserRepositoryInterface $user)
    {
        $this->user = $user;
    }

    public function deliverBeer()
    {
        return $this->user->all();
    }
}

UserRepository...

namespace Cartalini\Drayman\Repositories;

class UserRepository implements UserRepositoryInterface
{
    public function all()
    {
        return User::all();
    }
}

UserRepositoryInterface...

namespace Cartalini\Drayman\Repositories;

interface UserRepositoryInterface
{
    public function all();
}

Service provider...

public function register()
{
    $this->app->bind('Cartalini\Drayman\Repositories\UserRepositoryInterface', 'Cartalini\Drayman\Repositories\UserRepository');
}

And finally my controller...

use Cartalini\Drayman\Drayman as Drayman;

class HomeController extends BaseController 
{
    public function showWelcome()
    {
        $drayman = new Drayman;
        return $drayman->deliverBeer();
    }
}

Can anyone help me to debug this please?

Upvotes: 0

Views: 7938

Answers (2)

Faramarz Qoshchi
Faramarz Qoshchi

Reputation: 1632

it might be a to late, but i had the same problem and the reason was my concrete class didn't implement it's corresponding interface class. after implementing it, all went well.
although you are correctly implementing it, so this error might have a few reason, which one of them is what i described.

Upvotes: 1

qwertynl
qwertynl

Reputation: 3933

In your showWelcome function:

public function showWelcome()
{
    // need to pass a UserRepositoryInterface object here:
    $drayman = new Drayman;
    return $drayman->deliverBeer();
}

Since you did not pass a UserRepositoryInterface object that your code requires you get that error.

Upvotes: 1

Related Questions