当我尝试将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
答案 0 :(得分:0)
该错误表示正在期望实现PaymentGatewayInteface
的类。
为此,您需要明确地说一个类正在实现一个接口,就像extending
一个类时一样:
class StripePaymentGateway implements PaymentGatewayInteface