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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:46:13+00:00 2026-05-22T17:46:13+00:00

If objects are passed by reference in PHP5, then why $foo below doesn’t change?

  • 0

If objects are passed by reference in PHP5, then why $foo below doesn’t change?

$foo = array(1, 2, 3);
$foo = (object)$foo;

$x = $foo;            // $x = &$foo makes $foo (5)!
$x = (object)array(5);

print_r($foo); // still 1,2,3

so:

Passing by reference not the same as
assign.

then why $foo below is (100, 2, 3) ?

$foo = array('xxx' => 1, 'yyy' => 2, 'zzz' => 3);
$foo = (object)$foo;

$x = $foo;            
$x->xxx = 100;

print_r($foo);
  • 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-22T17:46:14+00:00Added an answer on May 22, 2026 at 5:46 pm

    The problem lies here:

    $x = $foo;   
    $x = (object)array(5);
    

    On the first rule $x is referenced to $foo; editing $x wil also edit $foo;
    (this is called “assign by reference”, not “pass by reference” *1)

    $x->myProperty= "Hi";
    

    Will cause $foo to also have a property “myProperty”.

    But on the next line you reference $x to a new object.
    Effectively unreferencing $x from $foo, all changes you make to $x won’t propogate to $foo.

    *1: When you call a function, the objects you pass to the functions are (in php5) “passed by reference”

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

Sidebar

Related Questions

It is well documented that PHP5 OOP objects are passed by reference by default.
In php 5, all variable and objects are passed by reference, but i can't
It appears that in PHP objects are passed by reference. Even assignment operators do
Are ColdFusion objects (i.e. CFCs invoked via cfobject) normally passed by reference or by
I just found out the hard way objects are passed by reference in Javascript,
Haskell doesn't feature explicit memory management, and all objects are passed by value, so
I always thought that objects where always passed as reference in C# and that
I'm confused about constant objects in C++ When we have passed a constant object/
I have a list of bean objects passed into my JSP page, and one
I've see a lot of MVC examples where domain-objects are passed directly to views,

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.