Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:26:16+00:00 2026-06-12T16:26:16+00:00

Summary I’m trying to find a way to change a referenced variable two levels

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:26:17+00:00Added an answer on June 12, 2026 at 4:26 pm

    MongoCollection::save() and MongoCollection::insert() can both modify their argument by setting an _id key, although it doesn’t seem to be documented for save() (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:

    public function save(&$entry)
    {
        if (empty($entry)) {
            return false;
        }
    
        if (!$this->checkSchema($entry)) {
            $this->throwDbError('Schema violation in `' . get_class($this) . '`');
        }
    
        try {
            $entryCopy = $entry;
            $saveResult = $this->collection->save($entryCopy);
    
            if (!isset($entry['_id']) && isset($entryCopy['_id']) {
                $entry['_id'] = $entryCopy['_id'];
            }
    
            return $saveResult; 
        } catch (Exception $e) {
            return $this->throwDbError($e);
        }
    }
    

    I suppose you could always copy the _id property back to $entry if 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary: I am trying to determine the easiest way to mask the output of
Summary : is there a way to get the unique lines from a file
Summary I am after some advice on the easiest way to analyze simple data
Summary: I'm trying to write a text string to a column of type varchar(max)
SUMMARY: The goal is to find the path to the WEB-INF folder in the
Summary jquery is used to retrieve search results via the get() call. When rendering
Summary: I have a bunch of C functions I have to call from C#.
Summary: I want to save two classes of the same name and different namespaces
Summary Amazingly I could find nothing about this on Google or SO. When I
Summary: Trying to dynamically add heading rows to a ListView via a custom adapter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.