The right way to get a lot of data in object oriented PHP5

Fortunately, I know how to retrieve data from a database. It's not a problem. For my object oriented application, I will have a table with users / people. I also have a class person.

Case: I would like to show the end user a list with all the people. What is the right way to show this?

  • using mysql_fetch_object(), in this case php will create its own type of object, not my own typeperson
  • Extract all rows from db and then create an object of each of them?

Or is there another better way to do this?

Can you also show some (pseudo) code?

thanks

+3
source share
3 answers

The function mysql_fetch_object()takes a second parameter, which is used to use the class name:

$myPerson = mysql_fetch_object($result, 'person');
+6
source
  • option 1: use ORM (Propeler, Doctrine)
  • option 2: create a People class that will load it with mysql_fetch_*, and then create an array of Person objects. Deposit IteratorAggregatewith getIteratorrefundArrayIterator(personArray)
+3
source

If you use PDO , $pdoStatement->fetchObject($type)selects one row as an instance of $typeClass

To get all objects as an instance of a class $type, use$pdoStatement->fetchAll(PDO::FETCH_CLASS, $type)

+2
source

Source: https://habr.com/ru/post/1705630/


All Articles