Skip to content

Instantly share code, notes, and snippets.

@cpriest
Created January 5, 2013 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpriest/4463107 to your computer and use it in GitHub Desktop.
Save cpriest/4463107 to your computer and use it in GitHub Desktop.
Description of current accessors guarding
<?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