Summary
I’m trying to find a way to change a referenced variable two levels up whilst avoiding Deprecated: Call-time pass-by-reference has been deprecated
Research I’ve done
I’ve looked through this and this and it seems like call_user_func_array can silence the warning however I think I’m missing something.
The Problem
I’m using MongoDB with PHP, the following method belongs to a model and simply checks the schema of the input that is passed into it by reference before saving it.
// $this->collection is the MongoCollection object
public function save(&$entry) {
if( empty($entry) ) return false;
if( !$this->checkSchema($entry) ) $this->throwDbError('Schema violation in `' . get_class($this) . '`');
try { return $this->collection->save(&$entry); } // <---- want to avoid using &
catch (Exception $e) { return $this->throwDbError($e); }
}
MongoCollection::save ($this->collection->save) will append the _id field onto $entry with the new document id. However this change isn’t being reflected on the $entry that is passed into the method above unless I pass it call time by reference. (Essentially I want MongoCollection::save to be able to modify $entry two levels up)
All right, that’s my best shot at explaining the problem, let me know if you need clarification.
MongoCollection::save() and MongoCollection::insert() can both modify their argument by setting an
_idkey, although it doesn’t seem to be documented forsave()(I’ll fix that soon). Internally, both methods are modifying the raw zval passed to the C functions. If I had to guess, this is because specifying the first argument as a reference would make it impossible to pass array literals. So, the extension cheats and modifies the argument anyway, with the side effect of being unable to modify something passed by reference.I tested the following code, which seems to work around this at the expense of copying the array argument in your save method:
I suppose you could always copy the
_idproperty back to$entryif you like. Alternatively, you could do with the array copying and simply initialize$entry[_id]to a new MongoId instance if it’s not already set. That’s essentially what the driver does for you when inserting a document without an_id.