Hi I'm getting an error of should be an instance of interface
App\Repositories\Traits\PhotoService::__construct() must be an instance of AwsServiceInterface, instance
Here's what I have so far
namespace App\Repositories\Interfaces;
interface AwsServiceInterface
{
//...
}
Now I have this class
namespace App\Repositories\Interfaces;
use App\Repositories\Interfaces\AwsServiceInterface;
class CloudFrontService implements AwsServiceInterface
{
public function __construct()
{
}
}
Now I'm using a dependency injection on this class
namespace App\Repositories\Traits;
use App\Repositories\Interfaces\AwsServiceInterface;
class PhotoService
{
protected $service;
public function __construct(AwsServiceInterface $service)
{
$this->service = $service;
}
public function getAuthParams($resource, $search_key = '')
{
// Execute a function from a class that implements AwsServiceInterface
}
And I'm calling the PhotoService class like this
$photo_service = new PhotoService(new CloudFrontService());
echo $photo_service->getAuthParams($resource);
But somehow I'm getting this error
FatalThrowableError: Type error: Argument 1 passed to App\Repositories\Traits\PhotoService::__construct() must be an instance of AwsServiceInterface, instance of App\Repositories\Interfaces\CloudFrontService given
I solved my problem. For those of you having the same issue make sure you do this step. 1. as mentioned by @Amit double check on the service provider that the service is bind under register function 2. Make sure to do
You are having a problem with namespacing. The typehint that you are using isn't complete for what you are looking for.
Just guessing but I think that you want to change the typehint to be:
public function __construct(App\Repositories\Interfaces\AwsServiceInterface $service)
You need to bind implementation to that interface. Here's an example.
Laravel itself does not know what implementation you want to use for this interface, you need to specify that yourself.
In your App\Providers\AppServiceProvider
class add following code in register()
method:
$this->app->bind(
'App\Repositories\Interfaces\AwsServiceInterface',
'App\Repositories\Interfaces\CloudFrontService'
);
and then you can use it as:
$photo_service = app(PhotoService::class);
echo $photo_service->getAuthParams($resource);