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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:21:33+00:00 2026-05-23T03:21:33+00:00

Please save me from myself (or reassure me that I’m not being completely misguided)

  • 0

Please save me from myself (or reassure me that I’m not being completely misguided)

I’ve gotten into the habit of writing code something like the following:

function foo($aUserObject) {
    $theUserUID = $aUserObject->uid;
    $aDeepValue = $aUserObject->property[123][456];
    [more code, in which much use is made of $theUserUID and $aDeepValue]
}

My strategy is probably obvious: I’m taking the attitude that it’s going to be easier for the PHP interpreter to handle a variable reference than to continually dig into the object to find the thing I’m interested in, so I should be getting some performance benefit. In addition, my code is perhaps a bit more bug-free and understandable (as long as I remember the meanings of the variable names), since I’m mostly writing simple variable names instead of longer and more complex object/array references where my fingers are more likely to slip. I understand that there’s a price for doing this — there are now two copies of $aUserObject->uid and $aUserObject->property[123][456] floating around, and if those values are large, the additional memory costs could add up. But I’m currently willing to pay that price in exchange for the (alleged) benefits.

Or, that’s what I’m telling myself anyway, based on my naive theory of how PHP underpinnings work. But reality, especially when opcode caching tools like APC get introduced, may be a totally different matter. Any more informed opinions out there, that might push me one way or another?

Thanks!

  • 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-23T03:21:34+00:00Added an answer on May 23, 2026 at 3:21 am

    I can reassure you, you’re wrong when you say:

    I understand that there’s a price for doing this — there are now two copies of $aUserObject->uid and $aUserObject->property[123][456] floating around, and if those values are large, the additional memory costs could add up.

    Unless $theUserUID is modified or referenced, it points to the exact same memory location that the property you fetched it from.

    You can even do:

    $a = $b = $c = $d = $e = 'hello world!';
    

    And it won’t take any more memory than:

    $a = 'hello world!';
    

    A copy will be created in the following scenarios:

    $a = 1;
    $b = $a;  // $b references $a
    $b = 2;   // $b is now a copy (no longer references $a)
    
    $a = 1;
    $b = $a;  // $b references $a
    $c = &$b; // $b is now a copy (no longer references $a)
    

    It’s called copy-on-write.

    Tip: Try debug_zval_dump and memory_get_usage and notice the refcount that increases, while the memory usage stays the same.

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

Sidebar

Related Questions

I'm completely new to sql and can't do that myself. So I need your
Please save me from going totally mad here. With my project, I keep finding
In iPhone Apps, I want to take pictures from Camera & save into Photo
I need to save NSStrings from an array into a .txt file in my
Please suggest me a method to save an XML file to the current installation
Please refer to the topic http://www.codeproject.com/KB/viewstate/SaveViewState.aspx . The topic demonstrates how you can save
Please have a look on the code below .. <div id=click_me>Save</div> <div id=blocks_sortable> <div
Please have a look at http://500px.com/photo/3977429?from=popular . Down in the right corner, theres a
I'm looking to save myself some effort further down the line by making a
Could someone please save these 2 files and run them and tell me why

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.