在php中对其实现的对象的接口引用

时间:2014-06-21 17:47:44

标签: php interface

我正在尝试在PHP中指向其实现对象的接口引用。 这是我的尝试

这是班级:

class Account implements FDInterface
{
    public $bal;
    public function Account() 
    {
        $bal = 0;
        echo 'Account created with balance '.$bal;
    }

    public function getFDInterest()
    {
        echo '</br> Interest Rate is 9.85';
    }
}

界面:

interface FDInterface 
{
    public function getFDInterest();
}

Index.php

中存在真正的问题
FDInterface fdAcc = new Account();   // this is line 1
$fdAcc->getFDInterest();

我得到的输出是

  

语法错误,第1行意外的T_STRING Index.php

1 个答案:

答案 0 :(得分:2)

您无法使用FDInterface fdAcc = new Account();。在这种情况下,数据类型接口只能用作函数中的参数:

function callSometing(FDInterface $fdAcc) {
     $fdAcc->something();
}

在您的情况下,正确和功能代码是:

$fdAcc = new Account();
相关问题