Problem is, as far as I can tell, in scoping.
In methoddemo()
I'm calling methodremoveElement()
which removes an element from the array.
Problem is whenremoveElement()
method removes soldier from array it's only removed in that method's scope, when I check that array indemo()
method after$this->removeElement();
line, the array remains unchanged.
I've tried to return$elements
array inremoveElement()
method but that doesn't help either and it that case it returns only array not entire updated object.
I've reduced the problem to 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');
}
Which outputs:
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.
I would expect$foo->elements
to have one element less after going throughDemo::removeElement()
.
The array is not returned by reference, so it's not changed "in place". This has nothing to do with OOP and classes, it's basic understanding of how functions and return values work.
You have, basically, three options
Expose asetElements(array $elements)
in yourFoo
class. You could call this in yourremoveElement()
method. (see it working here)
private function removeElement(): void
{
$elements = $this->foo->getElements();
array_splice($elements, 2, 1);
echo "\nWe have ", count($elements), " soldiers in local \$elements\n";
$this->foo->setElements($elements);
}
Since you want to change the contents ofFoo::elements
from outside, you could just as well make the property public.public array $elements;
, and get rid ofgetElements()
altogether. See it working here.
MakegetElements()
return a reference, which is what it appears you thought was happening. Seems overkill to me, but it would be an easy "fix".
public function &getElements():array {
return $this->elements;
}
// and remember to access the dereference the return when using it:
// $elements = & $this->foo->getElements();
See it working here
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.