PHP - 在接口类中使用__call函数

时间:2016-02-18 10:57:58

标签: php oop

将魔术函数__call作为一个抽象并包含在接口类中是否可以将所有实现类绑定到包含__call函数? 感谢

2 个答案:

答案 0 :(得分:1)

在你询问是否可以做某事之前,也许更容易做一些测试。

$ php -r '
abstract class Foo{
  public abstract function __call($a,$b);
}
class Bar extends Foo{}
'
PHP Fatal error:  Class Bar contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
     第5行的命令行代码中的

(Foo :: __ call)

$ php -r '
interface Foo{
  public function __call($a,$b);
}
class Bar implements Foo{}
'
PHP Fatal error:  Class Bar contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
     第5行的命令行代码中的

(Foo :: __ call)

答案 1 :(得分:0)

接口是实现它的任何类必须满足的合同。 如果接口包含__call()方法,那将是什么样的合同?

接口保证了类将实现某些指定的功能。 __call()不是指定的功能'。它缺乏它。

所以,即使有可能,这肯定是你不应该做的事情。