gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:36:05 24 4
gpt4 key购买 nike

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 允许它?

最佳答案

PhpStorm 警告您,您的代码可能允许将 Base 的实例添加到 BaseService::process,但该实例不是有效的 Child 实例,因此不能传递给 ChildService::process

在您的第一个单元测试中,您提供了 Child 的一个实例,它扩展了 Base,所以它起作用了。

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

如果 BaseService::process总是调用 ChildService::process 就像您现在所做的那样,那么 BaseService::process 应该类型提示其参数也与 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());

关于php - 调用参数类型与声明的不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37888248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com