Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is exactly happening when instantiating with 'new'?
    text
    copied!<p>Let's consider the following code:</p> <pre><code>class a { public $var1; function disp(){ echo $this-&gt;var1; } } $obj1 = new a; echo '&lt;br/&gt;After instantiation into $obj1:&lt;br/&gt;'; xdebug_debug_zval('obj1'); $obj1-&gt;var1 = "Hello "; echo '&lt;br/&gt;&lt;br/&gt;After assigning "Hello" to $obj-&gt;var1:&lt;br/&gt;'; $obj1-&gt;disp(); echo "&lt;br/&gt;&lt;br/&gt;"; xdebug_debug_zval('obj1'); </code></pre> <p>The output:</p> <blockquote> <p>After instantiation into $obj1:<br/> obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=2, is_ref=0)=NULL }</p> <p>After assigning "Hello" to $obj->var1:<br/> Hello</p> <p>obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=1, is_ref=0)='Hello ' }</p> </blockquote> <p>One by one:</p> <blockquote> <p>After instantiation into $obj1:<br/> obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=2, is_ref=0)=NULL }</p> </blockquote> <p>Why does <code>$obj1-&gt;var1</code> have <code>refcount=2</code> when there is only one object of class a?</p> <p>Is it because of how the <code>new</code> operator makes assignment? PHP does assignment with references. When instantiated with <code>new</code>, no symbol/variable name is associated with that instance. But, the class properties do have names. Is the <code>recount=2</code> because of this?</p> <p>If that is the case then a C.O.W (copy on write) has occurred with a shallow copy WRT the class instance. While the properties are still pointing to the zval's of properties created during the instantiation using <code>new</code>.</p> <p>Now,</p> <blockquote> <p>After assigning "Hello" to $obj->var1:<br/> Hello</p> <p>obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=1, is_ref=0)='Hello ' }</p> </blockquote> <p>So, when I assign a value to the property <code>$obj1-&gt;var1</code> a new zval container for that property and hence the <code>refcount=1</code>?</p> <p><em>Does this mean that the zval container created during instantiation using <code>new</code> still lives but cannot be accessed since there is no symbol / variable name associated with it?</em></p> <p>Please note (from <a href="http://xdebug.org/docs/display#xdebug_debug_zval" rel="nofollow">xdebug: Variable Display Features</a>): <br/> <code>debug_zval_dump()</code> is different from <code>xdebug_debug_zval()</code>.</p> <blockquote> <p><em><strong>void</em> xdebug_debug_zval( <em>[string varname [, ...]]</em> )</strong></p> <p><em>Displays information about a variable</em>.</p> <p>This function displays structured information about one or more variables that includes its type, value and refcount information. Arrays are explored recursively with values. This function is implemented differently from PHP's <a href="http://php.net/debug-zval-dump" rel="nofollow">debug_zval_dump()</a> function in order to work around the problems that that function has because the variable itself is actually passed to the function. Xdebug's version is better as it uses the variable name to lookup the variable in the internal symbol table and accesses all the properties directly without having to deal with actually passing a variable to a function. The result is that the information that this function returns is much more accurate than PHP's own function for showing zval information. </p> </blockquote> <h3><em><strong><code>UPDATE</code></em></strong> : <code>Dec 31th 2011:</code></h3> <blockquote> <p>I am trying to look at how memory allocation takes place when <strong>new</strong> is used. But There are too many other things I have to do right now. I hope I will be able to post an useful update soon. Until then here are the links to code at which I was looking at :</p> <ul> <li><a href="http://lxr.php.net/opengrok/xref/PHP_5_3/main/alloca.c#137" rel="nofollow">typedef union hdr</a><br/></li> <li><a href="http://lxr.php.net/opengrok/xref/PHP_5_3/main/alloca.c#202" rel="nofollow">register pointer new = malloc (sizeof (header) + size);</a> <br/></li> <li><a href="http://lxr.php.net/xref/PHP_5_3/Zend/zend_language_parser.y#579" rel="nofollow">expr_without_variable: T_NEW class_name_reference</a><br/></li> <li><a href="http://lxr.php.net/xref/PHP_5_3/Zend/zend_compile.c#zend_do_fetch_class" rel="nofollow">function zend_do_fetch_class</a><br/></li> </ul> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload