Let’s say you have a class, with certain properties, and that you tried your best so those properties would match the column names in a database in a way that you could fit each row of a db into your class object.
The way I usually do this, is by creating a method $class->get_all() to query the database for all the rows that match a specific query. From the resource set, I then create several sql objects, with mysql_fetch_object($resource) and store them in one array which is then returned. Basically something like this usually happens:
<?php
while($row = mysql_fetch_object($result_set)){
$class->id = $row->id;
$class->name = $row->name;
$class->birthdate = $row->birthdate;
$output[] = $class;
}
return $output;
?>
… which means I have 2 objects ($row and $class), with the same properties, and I am copying one of them into the other.
Is there a way to optimize this? Like, instead of copying the values from one to the other, maybe set them as a pointer to the same sql object property?
With PDO:
Note: this is a simplified example. In production code, you may need to take user-input into account, in which case you use prepared statements. That does not change the way you should fetch the results.