title: How to call function from object stored in array in PHP?
body: <p>I can't call / access the Id function from Product class which is stored in <code>$products</code> array in the <code>foreach</code> loop. (<code>$product-&gt;Id()</code>)</p>
  <p>I have tried</p>
  <pre><code>class Product
  {
    private $id;
  
    public function __contruct($id)
    {
      $this-&gt;Id($id);
    }
  
    public function Id($value = '')
    {
      if (empty($value)) {
        return $this-&gt;id;
      } else {
        $this-&gt;id = $value;
      }
    }
  }
  </code></pre>
  <pre><code>function testArray()
  {
    global $conn;
  
    $searchQuery = &quot;SELECT * FROM products&quot;;
    $stmt = $conn-&gt;prepare($searchQuery);
    $stmt-&gt;execute();
    $result = $stmt-&gt;get_result();
  
    $products = array();
  
    while ($row = $result-&gt;fetch_assoc()) {
      $products[] = new Product(
        $row[&quot;id&quot;]
      );
    }
  
    foreach ($products as $product) {
      print_r($product-&gt;Id());
    }
  
    $stmt-&gt;close();
  }
  </code></pre>