Created
February 19, 2014 20:55
-
-
Save lyrixx/9101326 to your computer and use it in GitHub Desktop.
Ensure visibility of a property does not change when unseted
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 Foobar | |
{ | |
private $foo = 'start'; | |
public function getFoo() | |
{ | |
return $this->foo; | |
} | |
public function unsetFoo() | |
{ | |
unset($this->foo); | |
} | |
public function __get($ppt) | |
{ | |
$this->$ppt = 'magic'; | |
return $this->$ppt; | |
} | |
} | |
function display_visibility($object, $attr) | |
{ | |
$r = new ReflectionProperty($object, $attr); | |
switch (true) { | |
case $r->isPublic(): | |
return 'public'; | |
case $r->isProtected(): | |
return 'protected'; | |
case $r->isPrivate(): | |
return 'private'; | |
} | |
} | |
$o = new Foobar(); | |
var_dump($o->getFoo()); | |
var_dump(display_visibility($o, 'foo')); | |
$o->unsetFoo(); | |
var_dump($o->getFoo()); | |
var_dump(display_visibility($o, 'foo')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment