Created
July 24, 2012 17:15
-
-
Save niklasvh/3171278 to your computer and use it in GitHub Desktop.
__set__get_005.phpt
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 Test | |
{ | |
protected $x; | |
function __get($name) { | |
echo __METHOD__ . "\n"; | |
if (isset($this->x[$name])) { | |
return $this->x[$name]; | |
} | |
else | |
{ | |
return NULL; | |
} | |
} | |
function __set($name, $val) { | |
echo __METHOD__ . "\n"; | |
$this->x[$name] = $val; | |
} | |
} | |
class AutoGen | |
{ | |
protected $x; | |
function __get($name) { | |
echo __METHOD__ . "\n"; | |
if (!isset($this->x[$name])) { | |
$this->x[$name] = new Test(); | |
} | |
return $this->x[$name]; | |
} | |
function __set($name, $val) { | |
echo __METHOD__ . "\n"; | |
$this->x[$name] = $val; | |
} | |
} | |
$foo = new AutoGen(); | |
$foo->bar->baz = "Check"; | |
var_dump($foo->bar); | |
var_dump($foo->bar->baz); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment