Skip to content
Advertisement

php: call a method of an object coming from an array

I’m trying to figure out a way to call a method of a specified member (Class A) coming from an array of members in class B.

<?php
  class A
  {
    public function do_something()
    {
      echo "class A did something";
    }
  }

  class B
  {
    private $arr = array();
    private $current_index = 0;

    public function add_new_A()
    {
      $new_a = new A;
      array_push($this->arr, (object) [ 
        $this->current_index => $new_a
      ]);
      $this->current_index++;

    }

    public function get_an_A_by_index($index)
    {
      return $this->arr{$index};
    }  

    public function do_something_with_A_member_inside_array($index)
    {
      self::get_an_A_by_index($index)->do_someting();
    }
  }

  $b = new B;

  $b->add_new_a();
  $b->add_new_a();

  echo print_r($b->get_an_A_by_index(0));
  echo "n";

  $b->do_something_with_A_member_inside_array(0); // returns error 

  // console: 
  //   stdClass Object ( [0] => A Object ( ))
  //   Uncaught Error: Call to undefined method stdClass::do_something();

?>

To wrap things up, I want to know if my approach is considered bad and/or if there is something I can do to fix the error. Before you disagree with my code, take a look at my php code I’m actually working on. That’s all for my question.

Now about why I want to call a method of a member A inside . For my assignment I want a program that does something seperately with the method do_something of class a. So far, the only way to do that is by storing seperate members of A.

I’m not sure if I’m approaching this wrong or not, because I’m coming from Java. So, The first thing I came up with was the approach shown above. When I do this in Java, it works fine. But php is different from Java and I’m still learning how php works since I’m new to it.

Advertisement

Answer

Your code isn’t far off, you’ve just got an issue with the way you’re building up the collection of A objects.

array_push($this->arr, (object) [ 
    $this->current_index => $new_a
]);

This is creating a data structure that I’m pretty sure isn’t what you expect. You’ll end up with an array full of stdClass objects, each with a single A member and its own internal index:

Array
(
    [0] => stdClass Object
        (
            [0] => A Object
                (
                )

        )

)

You’re then retrieving the stdClass object and trying to run the method on that, hence the Call to undefined method stdClass::do_something… error you’re seeing.

Instead, all you need to do is this:

$this->arr[$this->current_index] = $new_a;

The rest of your code is just expecting an array of A objects, nothing nested any deeper.

I’ve put a full example here: https://3v4l.org/ijvQa. Your existing code had a couple of other typos, which are also fixed. You’ll spot them easily enough if you turn on error reporting.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement