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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:03:30+00:00 2026-06-17T00:03:30+00:00

I got a strange behaviour in PHP with arrays and objects, that I don’t

  • 0

I got a strange behaviour in PHP with arrays and objects, that I don’t understand. Maybe you guys can help me with that.

Creating an array, copy it to another array, change a value in the 2nd array and everything is as expected:

$array1['john']['name'] = 'foo';
$array2 = $array1;
$array2['john']['name'] = 'bar';

echo $array1['john']['name']; // foo
echo $array2['john']['name']; // bar

Now, if I do this with objects in that array, the object in the copied array holds some kind of a reference?

$array3['john']->name = 'foo';
$array4 = $array3;
$array4['john']->name = 'bar';

echo $array3['john']->name; // bar
echo $array4['john']->name; // bar

I would have expected the same behaviour as in the 1st example and I cannot find anything about this in the php docs. Can somebody explain that to me or send me an link to where this is documented?

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-06-17T00:03:31+00:00Added an answer on June 17, 2026 at 12:03 am

    Objects by default are passed by reference. If you assign some scalar value or an array to other variable, it is cloned. If you assign the object, only the reference is copied but the object is not.

    When assigning an already created instance of a class to a new
    variable, the new variable will access the same instance as the object
    that was assigned. This behaviour is the same when passing instances
    to a function. A copy of an already created object can be made by
    cloning it.

    From http://php.net/manual/en/language.oop5.basic.php

    So, you need to call clone if you want another object.

    $array4['john'] = clone $array3['john'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Even being relatively well aware of PHP peculiarities, the following strange behaviour still got
I got a strange behaviour with the XSD generator I can't really explain. I
I've got some strange behaviour happening I can't work out. I've written some code
I've got strange behaviour and I can't realize how to fix it. I've got
Recently my bash has often got into strange states I don't understand (debian squeeze
I've got very strange behaviour when I run playframework in scala. I used anorm
Question: Why there is extra space in 2.3.3? This is a strange behaviour that
I've got some strange behaviour with the following: Html <article class=feature> <div class=feature-media> <img
I've got a strange behaviour with an Oracle 9i database. If i make a
I've got a SQL-statement with a - for me not explainable - strange behaviour.

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.