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'로 사용할 수 있을지 모르겠다. –