Home > Blockchain >  Method doesn't change object array outside it's scope
Method doesn't change object array outside it's scope

Time:10-09

Problem is, as far as I can tell, in scoping.

In method demo() I'm calling method removeElement() which removes an element from the array.

Problem is when removeElement() method removes soldier from array it's only removed in that method's scope, when I check that array in demo() method after $this->removeElement(); line, the array remains unchanged.

I've tried to return $elements array in removeElement() 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 through Demo::removeElement().

CodePudding user response:

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 a setElements(array $elements) in your Foo class. You could call this in your removeElement() 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 of Foo::elements from outside, you could just as well make the property public. public array $elements;, and get rid of getElements() altogether. See it working here.

  • Make getElements() 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

  • Related