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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:59:31+00:00 2026-05-27T19:59:31+00:00

class a { public $test=msg1; } $t1 = new a; echo echo1: After Instantiation

  • 0
 class a {
public $test="msg1";
}          

 $t1 = new a;
 echo "echo1: After Instantiation :<br/>";
 xdebug_debug_zval('t1');echo "<br/><br/>";

 $t2 = $t1;
 echo 'echo2: After assigning $t1 to $t2 :<br/>';
 xdebug_debug_zval('t2');echo "<br/><br/>";

 $t1->test="msg2";
 echo 'echo3: After assigning $t1->test = "msg2" :<br/>';
 xdebug_debug_zval('t1');echo "<br/>";
 xdebug_debug_zval('t2');echo "<br/><br/>";

 $t2->test="msg3";
 echo 'echo4: After assigning $t2->test="msg3" :<br/>';
 xdebug_debug_zval('t1');echo "<br/>";
 xdebug_debug_zval('t2');echo "<br/><br/>"; 

 $t2->test2 = "c*ap!";
 echo 'echo5: After injecting $test2 to $t2 :<br/>';
 xdebug_debug_zval('t1');echo "<br/>";
 xdebug_debug_zval('t2');echo "<br/><br/>";

The output:

echo1: After Instantiation :
t1: (refcount=1, is_ref=0)=class a { public $test = (refcount=2, is_ref=0)=’msg1′ }

echo2: After assigning $t1 to $t2 :
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=2, is_ref=0)=’msg1′ }

echo3: After assigning $t1->test = “msg2” :
t1: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg2′ }
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg2′ }

echo4: After assigning $t2->test=”msg3″ :
t1: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg3′ }
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg3′ }

echo5: After injecting $test2 to $t2 :
t1: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg3′; public $test2 = (refcount=1, is_ref=0)=’cap!’ }
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg3′; public $test2 = (refcount=1, is_ref=0)=’c
ap!’ }

Ignoring echo1 & echo2 because of this: What is exactly happening when instantiating with ‘new’? & expected behaviour.

Considering echo3:

echo3: After assigning $t1->test = “msg2” :
t1: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg2′ }
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg2′ }

This is understandable as I am just changing $t1->test variable and no direct change to &t2->test.

Considering echo4, where a direct change to $t2->test is done:

echo4: After assigning $t2->test=”msg3″ :
t1: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg3′ }
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg3′ }

No C.O.W takes place! and the change is reflected to $t1 as well even though is_ref is not set.

Considering echo5, where the variable $test2 is injected into $t2:

echo5: After injecting $test2 to $t2 :
t1: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg4′; public $test2 = (refcount=1, is_ref=0)=’cap!’ }
t2: (refcount=2, is_ref=0)=class a { public $test = (refcount=1, is_ref=0)=’msg4′; public $test2 = (refcount=1, is_ref=0)=’c
ap!’ }

Again, no C.O.W takes place! and the change is reflected to $t1 as well even though is_ref is not set.

Why is this behaviour!?

  • 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-27T19:59:31+00:00Added an answer on May 27, 2026 at 7:59 pm

    It does but you’re having the wrong expectation.

    The value is an object identifier. You assign that to $t1 or $t2. The object identifier is copied on write, but it still refers to the same object, so the object isn’t copied in any of the cases you outline in your question.

    See Objects and references­Docs:

    One of the key-points of PHP 5 OOP that is often mentioned is that “objects are passed by references by default”. This is not completely true. […] As of PHP 5, an object variable doesn’t contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object.

    C.O.W. is an optimization. PHP here sees that $t1->test and $t2->test are actually the same value. So if you change it, the optimization kicks in in the sense that there is nothing to copy at all.

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

Sidebar

Related Questions

class test { public $isNew; public function isNew(){} } $ins = new test(); $ins->isNew
This is my class: public class Test { Test(){ new Webshop (new Warenkorb[]{Max, new
class test { public: test &operator=(const test & other){} // 1 const test &
Consider following class class test { public: test(int x){ cout<< test \n; } };
This code works: class Test { public: Test(string name) : _name(name) {}; bool operator()()
Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object
public class Test { public static void main(String[] args) { } } class Outer
class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
E,g class Test { public: void setVal(const std::string& str) { this.isVal = str; //This

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.