Skip to content

Instantly share code, notes, and snippets.

@tsnoad
Created July 9, 2010 06:30
Show Gist options
  • Save tsnoad/469134 to your computer and use it in GitHub Desktop.
Save tsnoad/469134 to your computer and use it in GitHub Desktop.
php's __get __set crazyness
<?php
//Define a simple class
class Foo {
//run this whenever a variable of this class is set
function __set($name, $value) {
//store the value we're setting in a completely arbitrary place
$this->morp = $value;
}
//run this whenever a variable of this class is getted!
function __get($name) {
//get the variable from the completely arbitrary place were it was kept
return $this->morp;
}
}
//start the madness!
$foo = new Foo();
//set a variable from the class with some text
$foo->skoo = "squiggle";
//make sure it was set
var_dump($foo->skoo);
assert($foo->skoo == "squiggle");
//make sure it was set some more
assert(!empty($foo->skoo));
//But this assertion fails
//WTFZ?
//so for the reasons of madness, we have to do this
$aaaaargh = $foo->skoo;
assert($aaaaargh == "squiggle");
assert(!empty($aaaaargh));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment