0

I can't call / access the Id function from Product class which is stored in $products array in the foreach loop. ($product->Id())

I have tried

class Product
{
  private $id;

  public function __contruct($id)
  {
    $this->Id($id);
  }

  public function Id($value = '')
  {
    if (empty($value)) {
      return $this->id;
    } else {
      $this->id = $value;
    }
  }
}
function testArray()
{
  global $conn;

  $searchQuery = "SELECT * FROM products";
  $stmt = $conn->prepare($searchQuery);
  $stmt->execute();
  $result = $stmt->get_result();

  $products = array();

  while ($row = $result->fetch_assoc()) {
    $products[] = new Product(
      $row["id"]
    );
  }

  foreach ($products as $product) {
    print_r($product->Id());
  }

  $stmt->close();
}

1 Answer 1

1

You misspelled

public function __contruct($id)

Change it to :

public function __construct($id)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.