PHP Designing Interfaces in PHP Introducing Interfaces Implement MySQL RepositoryInterface

Robert Anthony
Robert Anthony
19,315 Points

There does not seem to be enough information to complete this task - or am I missing something?

We have to implement the all() method of an interface which gets all the data from an SQL table passed to the function but it doesn't say whether this is just a string with the table name or something else. I tried running fetchAll(PDO::FETCH_OBJ) as suggested in the question but it want me to run a query on the database. To do this I believe I need a connection to the database but it doesn't tell me what the connection is or the information I need to establish my own connection.

sqlRepository.php
<?php
class sqlRepository extends PDO implements RepositoryInterface
{
  function all($table)
  {
    $db = 
    $results = $db->query("SELECT * FROM $table");
    return $results->fetchAll(PDO::FETCH_OBJ);
  }
}

1 Answer

Robert Anthony
Robert Anthony
19,315 Points

I finally worked out that I could call the PDO functions statically, so this worked:

    $stmt = PDO::query("SELECT * FROM $table");