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 7511619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:36:19+00:00 2026-05-29T23:36:19+00:00

I am wondering this question for a long time, how does PHP handle references

  • 0

I am wondering this question for a long time, how does PHP handle references are they a good idea to use and I can’t explain better than using an example, lets look at the following class and then @ the comment of the setResult method.

Lets imagine we are using a model view controller framework and we are building a basic AjaxController, we only got 1 action method (getUsers) so far. Read the comments, and I hope my question is clear, how does PHP handle these kind of situations and is it true what I wrote about the x times in the memory @ the setResult docblock.

class AjaxController{
    private $json = array(
        'result' => array(),
        'errors' => array(),
        'debug' => array()
    );

    /**
     * Adds an error, always displayed to users if any errors.
     * 
     * @param type $description 
     */
    private function addError($description){
        $this->json['errors'][] = $description;
    }

    /**
     * Adds an debug message, these are displayed only with DEBUG_MODE.
     * 
     * @param type $description 
     */
    private function addDebug($description){
        $this->json['debug'][] = $description;
    }

    /**
     * QUESTION: How does this go in memory? Cause if I use no references,
     * the array would be 3 times in the memory, if the array is big (5000+)
     * its pretty much a waste of resources.
     * 
     * 1st time in memory @ model result.
     * 2th time in memory @ setResult ($resultSet variable)
     * 3th time in memory @ $this->json
     *
     * @param array $resultSet 
     */
    private function setResult($resultSet){
        $this->json['result'] = $resultSet;
    }

    /**
     * Gets all the users
     */
    public function _getUsers(){
        $users = new Users();
        $this->setResult($users->getUsers());
    }

    public function __construct(){
        if(!DEBUG_MODE && count($this->json['debug']) > 0){
            unset($this->json['debug']);
        }

        if(count($this->json['errors']) > 0){
            unset($this->json['errors']);
        }

        echo json_encode($this->json);
    }
}

Another simple example: What would be better to use technique A:

function example(){
    $latestRequest = $_SESSION['abc']['test']['abc'];

    if($latestRequest === null){
        $_SESSION['abc']['test']['abc'] = 'test';
    }
}

Or technique B:

function example(){
    $latestRequest =& $_SESSION['abc']['test']['abc'];

    if($latestRequest === null){
        $latestRequest = 'test';
    }
}

Thanks for reading and advise 🙂

  • 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-05-29T23:36:20+00:00Added an answer on May 29, 2026 at 11:36 pm

    In short: don’t use references.

    PHP copies on write. Consider:

    $foo = "a large string";
    $bar = $foo; // no copy
    $zed = $foo; // no copy
    $bar .= 'test'; // $foo is duplicated at this point.
                    // $zed and $foo still point to the same string
    

    You should only use references when you need the functionality that they provide. i.e., You need to modify the original array or scalar via a reference to it.

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

Sidebar

Related Questions

This is a question I've been wondering about for a long time. How do
I've long been wondering about this question; say I first try to grep some
This is a question that I have been pondering for a long time ,
Inspired by this question I began wondering why the following examples are all illegal
This question on archiving PDF's got me wondering -- if I wanted to compress
Per this question (see comments near the bottom), I was wondering if anyone knows
This question asking for a phone number format API in Java got me wondering
This question is similar to this: Asp.net Profile Across Sub-Domain I'm basically wondering if
In answering this question ( https://stackoverflow.com/questions/352317/c-coding-question#352327 ), it got me wondering... Is there any
Reading through this question on multi-threaded javascript, I was wondering if there would be

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.