Проблема связующего интерфейса: аргумент 1, передаваемый __construct (), должен реализовывать интерфейс - PullRequest
1 голос
/ 04 мая 2019

У меня проблема при попытке привязать 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 Ответ

1 голос
/ 04 мая 2019

Ошибка говорит, что ожидает класс, который реализует PaymentGatewayInteface.
Для этого вам нужно явно сказать, что класс реализует интерфейс, как если бы extending класс:

class StripePaymentGateway implements PaymentGatewayInteface
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...