这就是我想要做的:
<?php
interface PaymentGatewayInterface {
public function pay(array $bill);
public function processNotification($notification);
}
class Payment {
protected $gateway;
public function __construct(PaymentGatewayInteface $gateway)
{
$this->gateway = $gateway;
}
public function pay(array $bill)
{
return $this->gateway->pay($bill);
}
public function processNotification($notification)
{
return $this->gateway->processNotification($notification);
}
}
class Paypal implements PaymentGatewayInterface {
public function pay(array $bill)
{
}
public function processNotification($notification)
{
}
}
$a = new Payment(new Paypal);
这是我从 PHP 收到的错误:
Catchable fatal error: Argument 1 passed to Payment::__construct() must be an instance of PaymentGatewayInteface, instance of Paypal given, called in /in/oP75h on line 43 and defined in /in/oP75h on line 14
在这里你可以自己测试一下:http: //3v4l.org/oP75h
我最初在 Laravel 4 和 Mockery (TDD) 中工作,但经过一段时间的调试后,我意识到这实际上“只是”一个 PHP 问题。