【发布时间】:2015-07-19 15:19:01
【问题描述】:
我正在尝试设置支付网关控制器并使用来自this url 的示例。
更具体地说,这里是他们的代码示例:
// Define the interface
interface BillingProvider {
public function charge($creditInfo);
}
// Create a Stripe implementation
class StripeBilling {
public function charge($creditInfo)
{
// Stripe_Charge::charge(...);
}
}
// Create a ServiceX implementation
class ServiceXBilling {
public function charge($creditInfo)
{
// charge user with ServiceX
}
}
然后是这个:
class PaymentController {
protected $billing;
public function __construct(BillingProvider $billing)
{
$this->billing = $billing;
}
}
如何在项目中正确实现这一点?
我正在尝试类似的东西:
$paymentController = new PaymentController(new StripeBilling);
但随后收到错误:
Catchable fatal error: Argument 1 passed to PaymentController::__construct() must implement interface BillingProvider, instance of StripeBilling given
做一些研究,似乎 StripeBilling 和 ServiceXBilling 都应该实现 BillingProvider,这是正确的吗?
【问题讨论】:
-
class StripeBilling extends BillingProvider -
是的,你是对的。
class StripeBilling implements BillingProvider和class ServiceXBilling implements BillingProvider -
好的,谢谢。请参阅下面约翰的回答中的更多 cmets。