Phpstormの検査結果はInvocation parameter types are not compatible with declared
です。呼び出しパラメータタイプは宣言と互換性がありません
私はphpがサブタイプとして使用ベースタイプを許可していることを驚いていました。
interface Base
{
public function getId();
}
interface Child extends Base
{
}
interface SecondChildType extends Base
{
}
class ChildImpl implements Child
{
public function getId()
{
return 1;
}
}
class SecondChildTypeImpl implements SecondChildType
{
public function getId()
{
return 2;
}
}
class BaseService
{
public function process(Base $base)
{
$childService = new ChildService($base);
return $childService->process($base); //Invocation parameter types are not compatible with declared
}
}
class ChildService
{
public function process(Child $child)
{
return $child->getId();
}
}
class InheritanceTest extends \PHPUnit_Framework_TestCase
{
public function testInterfacesCanUsesAsSubstitute()
{
$baseService = new BaseService();
$this->assertEquals(1, $baseService->process(new ChildImpl()));
}
/**
* @expectedException \TypeError
*/
public function testInterfacesCanUsesAsSubstitute_Exception()
{
$baseService = new BaseService();
$baseService->process(new SecondChildTypeImpl());
}
}
なぜ最初のテストに合格するのですか?なぜPHPはそれを許可したのですか?
なぜですか?実装が重要な理由 –
@ Onedev.Linkあなたが何を求めているのかわかりません – jszobody
'BaseService :: process'では' Base'オブジェクトだけを渡すことができます。そして 'ChildService :: process'に' Base'オブジェクトを 'Child'として渡します。 'Child'または' Child'サブタイプを 'ChildService :: process'で渡したいだけです。しかし、なぜ私は 'Base'を' Child'として使うことができるのか分かりません。 –