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?
mysql_*functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information.$row->idcontains the literal ID (an integer). It is passed by value, not by reference. If you were to pass the entire$rowobject, you would be correct. Your initial assumptions were correct.