Created
January 5, 2013 19:02
-
-
Save cpriest/4463107 to your computer and use it in GitHub Desktop.
Description of current accessors guarding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class a { | |
public $Foo { | |
get { echo 'G '; return $this->Foo; } | |
set { echo 'S '; $this->Foo = $value; } | |
isset { echo 'I '; return isset($this->Foo); } | |
unset { echo 'U '; unset($this->Foo); } | |
} | |
} | |
$o = new a(); | |
var_dump($o) | |
// object(a)#1 (1) { | |
// ["Foo"]=> | |
// NULL | |
// } | |
$o->Foo = 5; | |
// S <-- Note the echo in each of the accessors for call "tracing" | |
echo (int)isset($o->Foo); | |
// I G 1 <-- Note the first I (call to accessor), then isset() calls the getter (to get the value), finally returning true | |
unset($o->Foo); | |
// U <-- 2nd call to unset() does not call the unsetter (guarded) nor does it call the getter | |
var_dump($o) | |
// object(a)#1 (0) { | |
// } | |
// Note how the property is gone, it has been unset | |
$o->Foo = 5; | |
// S | |
var_dump($o) | |
// object(a)#1 (1) { | |
// ["Foo"]=> | |
// int(5) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment