What is exactly happening when instantiating with ‘new’?

Let’s consider the following code:

class a {
    public $var1;
    function disp(){
        echo $this->var1;
        }    
    }

$obj1 = new a;
echo '<br />After instantiation into $obj1:<br />';    
xdebug_debug_zval('obj1');  

$obj1->var1 = "Hello ";
echo '<br /><br />After assigning "Hello" to  $obj->var1:<br />';
$obj1->disp();

echo "<br /><br />";  
xdebug_debug_zval('obj1');  
 

The output:

After instantiation into $obj1:
obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=2, is_ref=0)=NULL }

After assigning "Hello" to $obj->var1:
Hello

obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=1, is_ref=0)='Hello ' }
 

One by one:

After instantiation into $obj1:
obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=2, is_ref=0)=NULL }
 

Why does $obj1->var1 have refcount=2 when there is only one object of class a?

Is it because of how the new operator makes assignment? PHP does assignment with references. When instantiated with new, no symbol/variable name is associated with that instance. But, the class properties do have names. Is the recount=2 because of this?

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 new.

Now,

After assigning "Hello" to $obj->var1:
Hello

obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=1, is_ref=0)='Hello ' }
 

So, when I assign a value to the property $obj1->var1 a new zval container for that property and hence the refcount=1?

Does this mean that the zval container created during instantiation using new still lives but cannot be accessed since there is no symbol / variable name associated with it?

Please note (from xdebug: Variable Display Features):
debug_zval_dump() is different from xdebug_debug_zval().

void xdebug_debug_zval( [string varname [, …]] )
 
    Displays information about a variable.
 
    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 debug_zval_dump() 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.

The question was posted by me on StackOverflow. Sadly couldn’t find an answer. If anyone could point to better Documenation of xdebug most welcome.
If you want to follow the conversation, here is the link to the question:
http://stackoverflow.com/q/8668664/858515

PHP : Demystify Type Hinting and Default Parameters

Type Hinting as described in PHP manual

PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

The above Means that:

Parameters which are Objects or Arrays can only be type hinted.
i.e.
Types (Scalar variables) integer, float, string or boolean & Types(non scalar) resource cannot be used in type hinting.

i.e.
The code in Code1 & Code2 are not allowed.

Code1:
function myfunc ( string $name = "myname") {  ...  }
Code2:
function myfunc ( int $age = 23) {  ...  }

Although the above will not generate any compile time error, It does give out a fatal error during runtime as.

Fatal error: Default value for parameters with a class type hint can only be NULL

This is because PHP would be treating string and int as class names.
To quote a valid example, lets say we have a class called myclass.
Then the code in code3 is allowed and right:

Code3:
function myfunc ( myclass $myobj){ ... }

The argument passed to function in code 3 must be an instance of ‘myclass’.

The final sentence which is:

if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

if default parameters are to be used use with type hinting, it can only have NULL as the default value.

Code4:
function myfunc ( myclass $myobj = NULL){ ... }

The argument passed to function in code 4 can be an instance of ‘myclass’ or NULL.

Code5:
function myfunc ( array $myarray = NULL){ ... }

The argument passed to function in code 4 can be an array or NULL.

You cannot instantiate an object in the parameter part of the function declaration!
&
The following function would be wrong!

Code6:
function myfunc ( array $myarray = array('a', 'b', 'c', 'd'){ ... }

A workaround:

Code7:
function myfunc ( array $myarray = NULL){ 
if ($myarray === NULL) $myarray = array('a', 'b', 'c', 'd');
}

*find any mistakes? do feel free to point it out.