调用参数类型与声明不兼容

One*_*ink 8 php

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么先测试通过?为什么php允许它?

jsz*_*ody 5

PhpStorm警告你,你的代码可能允许的实例BaseBaseService::process是不是一个有效的Child实例,因此不能被传递给ChildService::process

在您的第一个单元测试中,您提供了一个实例Child,它可以扩展Base,因此可以正常工作。

在第二个单元测试中,您实际上证明有可能导致PHP错误。PhpStorm只是在事先警告您,您的键入提示允许出现此问题。

如果BaseService::process总是调用ChildService::process像你现在所拥有的,那么BaseService::process应该typehint它的参数是兼容ChildService::process也是如此。


我已经稍微修改了您的代码,重写了一些类名称以使其更简单,并删除了该getId方法。我只是想尽可能简单地显示它,以帮助您了解正在发生的事情。

interface Base {}
interface Child extends Base {}
interface Base2 extends Base {}

// This class implements Child, which extends Base. So this will meet either requirement.
class Class1 implements Child {}

// This class implements Base2, which extends Base. 
// So this will meet any Base requirement, but NOT a Child requirement
class Class2 implements Base2 {}


class BaseService
{
    /**
     * Problem! We are requiring Base here, but then we pass the same argument to
     * ChildService->process, which requires Child. 
     * 
     * 1) Class1 WILL work, since it implements Child which extends Base.
     * 
     * 2) Class2 WILL NOT work. Or at least, we can't pass it to ChildService->process
     *    since it only implements Base2 which extends Base. It doesn't implement Child,
     *    therefore ChildService->process won't accept it.
     */
    public function process(Base $base)
    {
        $childService = new ChildService($base);

        return $childService->process($base);
    }
}

class ChildService
{
    /**
     * I will ONLY receive an instance that implements Child. 
     * Class1 will work, but not Class2.
     */
    public function process(Child $child)
    {
        return $child->getId();
    }
}

$service = new BaseService();

// I can do this! I'm passing in Child1, which implements Child, which extends Base.
// So it fulfills the initial Base requirement, and the secondary Child requirement.
$service->process(new Child1());

// I can't do this. While BaseService will initially accept it, ChildService will refuse
// it because this doesn't implement the Child interface as required.
$service->process(new Child2());
Run Code Online (Sandbox Code Playgroud)

  • @ Onedev.Link我不明白你在问什么 (2认同)