Difference between revisions of "ObjectsAndRefs"

From Hashphp.org
Jump to: navigation, search
Line 17: Line 17:
 
|-
 
|-
 
! colspan="2" style="text-align: left" | The above code creates a new class, SimpleClass, with no properties or methods. It then creates a new instance of this class
 
! colspan="2" style="text-align: left" | The above code creates a new class, SimpleClass, with no properties or methods. It then creates a new instance of this class
and attaches it to the variable named '$instance'.
+
and attaches it to the variable named '$instance'.
  
 
Because objects are "special" <ref>Sara Golemon, "You're Being Lied To" [http://blog.golemon.com/2007/01/youre-being-lied-to.html]</ref> in PHP5, the relationship between the variable '$instance' and the object inside it is not as direct as you might be expecting...
 
Because objects are "special" <ref>Sara Golemon, "You're Being Lied To" [http://blog.golemon.com/2007/01/youre-being-lied-to.html]</ref> in PHP5, the relationship between the variable '$instance' and the object inside it is not as direct as you might be expecting...

Revision as of 22:55, 26 July 2011

This page attempts to provide a visual guide to how objects - and references to them - work in PHP 5.0 and later.

class SimpleClass {
    public 
$var 'a default value';
}
$instance = new SimpleClass();
ObjRefImg0.png
The above code creates a new class, SimpleClass, with no properties or methods. It then creates a new instance of this class

and attaches it to the variable named '$instance'.

Because objects are "special" [1] in PHP5, the relationship between the variable '$instance' and the object inside it is not as direct as you might be expecting...

$assigned = $instance; ObjRefImg1.png
Explain the sample...
$instance->var = '$assigned will have this value as well'; ObjRefImg3.png
Explain the sample...
$reference =& $instance; ObjRefImg2.png
Explain the sample...
$instance = null; ObjRefImg4.png
Explain the sample...

Notes

  1. Sara Golemon, "You're Being Lied To" [1]