【发布时间】:2021-11-27 13:01:14
【问题描述】:
据我所知,问题在于范围界定。
在方法 demo() 中,我正在调用方法 removeElement(),它从数组中删除一个元素。
问题是当removeElement() 方法从数组中删除士兵时,它只在该方法的范围内被删除,当我在$this->removeElement(); 行之后检查demo() 方法中的数组时,数组保持不变。
我尝试在 removeElement() 方法中返回 $elements 数组,但这也无济于事,在这种情况下,它只返回数组而不是整个更新的对象。
我已将问题简化为this minimum example:
class Foo
{
private array $elements = [];
public function __construct(int $howMany)
{
for ($i = 0; $i < $howMany; $i++) {
$this->elements[] = random_int(1, 100);
}
}
public function getElements(): array
{
return $this->elements;
}
}
class Demo
{
public Foo $foo;
public function __construct(int $howMany)
{
$this->foo = new Foo($howMany);
}
public function demo(): void
{
echo "\nWe start with ", count($this->foo->getElements()), " soldiers in \$foo->elements\n";
$this->removeElement();
echo "\nWe have ", count($this->foo->getElements()), " soldiers in \$foo->elements\n";
}
private function removeElement(): void
{
$elements = $this->foo->getElements();
array_splice($elements, 2, 1);
echo "\nWe have ", count($elements), " soldiers in local \$elements\n";
}
}
$init = random_int(4,10);
$demo = new Demo($init);
$demo->demo();
$remaining = count($demo->foo->getElements());
if ($init-1 !== $remaining) {
throw new UnexpectedValueException('Wrong number of elements, application is broken');
}
哪些输出:
We start with 10 soldiers in $foo->elements
We have 9 soldiers in local $elements
We have 10 soldiers in $foo->elements
Fatal error: Uncaught UnexpectedValueException: Wrong number of elements, application is broken in /in/3cnhl:54
Stack trace:
#0 {main}
thrown in /in/3cnhl on line 54
Process exited with code 255.
我希望$foo->elements 在经过Demo::removeElement() 之后会少一个元素。
【问题讨论】: