I use mysqli extension and bind the result to an object:
class Item {
public $id, $name;
}
$Item = new Item;
$stmt->bind_result($Item->id, $Item->name);
Every call to $stmt->fetch()
will overwrite $Item's properties which became references. If I simply clone the object — these references remain and both instances change simultaneously:
object(Item)#1 (2) {
["id"]=>
&int(1)
["name"]=>
&string(4) "name"
}
So there should be a way to dereference them so clone
really makes a copy, not a set of referenced data. Is there a simple, flexible way to retrieve all the data so it's stored as an array of objects? The main problem is how to remove the references: &string
.
The only solution I see is to instantiate another object and manually copy all the properties:
$Item2 = new Item;
foreach ($Item as $prop => $val)
$Item2->$prop = $val;
but this seems to be an overkill: it will copy all properties (including some extra ones which were not actually referenced) which is not necessary, and moreover — is a penalty to the performance.