如何在PHP中正确键入接口?

时间:2013-07-28 16:16:34

标签: php laravel laravel-4 type-hinting

这就是我要做的事情:

<?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问题。

1 个答案:

答案 0 :(得分:3)

你有一个错字: -

public function __construct(PaymentGatewayInteface $gateway)
                                              ^ 'r' missing

应该是: -

public function __construct(PaymentGatewayInterface $gateway)

你错过了界面中的'r'。