Difference between revisions of "ObjectsAndRefs"

From Hashphp.org
Jump to: navigation, search
Line 18: Line 18:
 
! colspan="2" style="text-align: left" | The above code creates a new class, SimpleClass, with no properties or methods.<br />
 
! colspan="2" style="text-align: left" | The above code creates a new class, SimpleClass, with no properties or methods.<br />
 
It then creates a new instance of this class and attaches it to the variable named '$instance'.<br />
 
It then creates a new instance of this class and attaches it to the variable named '$instance'.<br />
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...
 
|-
 
|-
 
| <span  
 
| <span  
Line 27: Line 27:
 
| [[Image:ObjRefImg1.png]]
 
| [[Image:ObjRefImg1.png]]
 
|-
 
|-
! colspan="2" style="text-align: left" | Explain the sample...
+
! colspan="2" style="text-align: left" | For example, when we assign the variable '$instance' to a new variable '$assigned', what we end up with is the one object with two different labels.
 
|-
 
|-
 
|<span  
 
|<span  
Line 38: Line 38:
 
| [[Image:ObjRefImg3.png]]
 
| [[Image:ObjRefImg3.png]]
 
|-
 
|-
! colspan="2" style="text-align: left" | Explain the sample...
+
! colspan="2" style="text-align: left" | Even though we changed the property via the variable '$instance', it changed the underlying object, so both '$assigned' and '$instance' will have the new value.
 
|-
 
|-
 
|<span   
 
|<span   
Line 47: Line 47:
 
| [[Image:ObjRefImg2.png]]
 
| [[Image:ObjRefImg2.png]]
 
|-
 
|-
! colspan="2" style="text-align: left" | Explain the sample...
+
! colspan="2" style="text-align: left" | '$reference', on the other hand, is a reference to the variable '$instance', and is NOT attached directly to the underlying object.
 
|-
 
|-
 
|<span   
 
|<span   
Line 56: Line 56:
 
| [[Image:ObjRefImg4.png]]
 
| [[Image:ObjRefImg4.png]]
 
|-
 
|-
! colspan="2" style="text-align: left" | Explain the sample...
+
! colspan="2" style="text-align: left" | So when we assign NULL into '$instance', we haven't changed the object (it's still available via '$assigned'), but both '$instance' and '$reference' have lost their attachment to it.
 
|}
 
|}
  
 
===Notes===
 
===Notes===
 
{{Reflist}}
 
{{Reflist}}

Revision as of 23:06, 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
For example, when we assign the variable '$instance' to a new variable '$assigned', what we end up with is the one object with two different labels.
$instance->var = '$assigned will have this value as well'; ObjRefImg3.png
Even though we changed the property via the variable '$instance', it changed the underlying object, so both '$assigned' and '$instance' will have the new value.
$reference =& $instance; ObjRefImg2.png
'$reference', on the other hand, is a reference to the variable '$instance', and is NOT attached directly to the underlying object.
$instance = null; ObjRefImg4.png
So when we assign NULL into '$instance', we haven't changed the object (it's still available via '$assigned'), but both '$instance' and '$reference' have lost their attachment to it.

Notes

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