Php 更严格的子接口
我有一个特殊的用例,我希望子接口在其返回值上有父接口没有的约束。例如:Php 更严格的子接口,php,inheritance,interface,Php,Inheritance,Interface,我有一个特殊的用例,我希望子接口在其返回值上有父接口没有的约束。例如: interface FooInterface { /** * @return mixed */ public function foo(); } interface IntegerOnlyFooInterface extends FooInterface { /** * @return integer */ public function foo();
interface FooInterface
{
/**
* @return mixed
*/
public function foo();
}
interface IntegerOnlyFooInterface extends FooInterface
{
/**
* @return integer
*/
public function foo();
}
在本例中,IntegerOnlyFoo接口将仅为调用foo返回整数。因为整数也是标量,这不会破坏从FooInterface继承的契约,并且满足,因为IntegerOnlyFooInterface可以在任何需要FooInterface实例的地方使用,而不会破坏应用程序中的任何内容
不过,在某些地方,我希望强制执行整数返回值作为代码契约的一部分,因此在这些地方,我会键入integeronlyfoo接口的提示
不过,这似乎有点含糊不清,我想知道我是不是用错误的方式解决了这个问题,还是说这个问题一直在向上发展