嗨,我收到了一个应该是接口实例的错误
App\Repositories\Traits\PhotoService::__construct() must be an instance of AwsServiceInterface, instance到目前为止,我的情况如下
namespace App\Repositories\Interfaces;
interface AwsServiceInterface
{
//...
}现在我有了这门课
namespace App\Repositories\Interfaces;
use App\Repositories\Interfaces\AwsServiceInterface;
class CloudFrontService implements AwsServiceInterface
{
public function __construct()
{
}
}现在我在这个类上使用了依赖项注入
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
}我像这样调用PhotoService类
$photo_service = new PhotoService(new CloudFrontService());
echo $photo_service->getAuthParams($resource);但不知怎么的,我得到了这个错误
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发布于 2017-01-26 23:55:41
我解决了我的问题。对于有同样问题的人,请确保执行此步骤。1.如@Amit所述,在注册函数2下对服务提供者进行二次检查,确认服务是绑定的。
发布于 2017-01-25 16:12:52
在App\Providers\AppServiceProvider类中,在register()方法中添加以下代码:
$this->app->bind(
'App\Repositories\Interfaces\AwsServiceInterface',
'App\Repositories\Interfaces\CloudFrontService'
);然后你可以把它用作:
$photo_service = app(PhotoService::class);
echo $photo_service->getAuthParams($resource);发布于 2017-01-25 15:09:28
您在名称空间上遇到了问题。您正在使用的类型提示对于您要查找的内容没有完成。
只是猜测一下,但我认为您想要更改类型为:
public function __construct(App\Repositories\Interfaces\AwsServiceInterface $service)http://php.net/manual/en/language.namespaces.basics.php
https://stackoverflow.com/questions/41854849
复制相似问题