绑定接口问题:传递给__construct()的参数1必须实现接口

时间:2019-05-04 16:57:33

标签: laravel

当我尝试将StripePaymentGateway绑定到PaymentGatewayInteface时遇到问题

namespace App\Billing;

interface PaymentGatewayInteface
{
    public function charge($amount, $token);
}
namespace App\Billing;

use Stripe\Charge;

class StripePaymentGateway
{
    private $apiKey;

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

    public function charge($amount, $token)
    {
        // code
    }
}

我的AppServiceProvider:

namespace App\Providers;

use App\Billing\StripePaymentGateway;
use App\Billing\PaymentGatewayInteface;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(StripePaymentGateway::class, function () {
            return new StripePaymentGateway(config('services.stripe.secret'));
        });

        $this->app->bind(PaymentGatewayInteface::class, StripePaymentGateway::class);
    }
}
namespace App\Http\Controllers;

use App\Billing\PaymentGatewayInteface;

class ConcertsOrdersController extends Controller
{
    private $paymentGateway;

    public function __construct(PaymentGatewayInteface $paymentGateway)
    {
        $this->paymentGateway = $paymentGateway;
    }
}

此错误显示:

Symfony\Component\Debug\Exception\FatalThrowableError  : Argument 1 passed to App\Http\Controllers\ConcertsOrdersController::__construct() must implement interface App\Billing\PaymentGatewayInteface, instance of App\Billing\StripePaymentGateway given

1 个答案:

答案 0 :(得分:0)

该错误表示正在期望实现PaymentGatewayInteface的类。
为此,您需要明确地说一个类正在实现一个接口,就像extending一个类时一样:

class StripePaymentGateway implements PaymentGatewayInteface
相关问题