Last active
August 29, 2015 14:02
-
-
Save TheFox/49ff6903da287c30e72f to your computer and use it in GitHub Desktop.
PHP Exportable Class
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 | |
/* | |
* @author Christian Mayer <http://fox21.at> | |
* @link http://stackoverflow.com/q/21762276/823644 | |
* @link https://eval.in/163041 | |
* @link https://eval.in/163462 | |
* @link https://eval.in/163909 | |
* @link https://gist.github.com/TheFox/49ff6903da287c30e72f | |
*/ | |
interface Exportable{ | |
public function __get_state(); | |
} | |
function unset_with_get_state($expression){ | |
$before = clone $expression; | |
$classVars = array_keys(get_class_vars(get_class($before))); | |
foreach(array_diff($classVars, $before->__get_state()) as $var){ | |
unset($before->$var); | |
} | |
return $before; | |
} | |
function my_var_export($expression, $return = null){ | |
$before = $expression; | |
if($before instanceof Exportable){ | |
$before = unset_with_get_state($expression); | |
} | |
return var_export($before, $return); | |
} | |
class Foo implements Exportable{ | |
public $name = null; | |
public $foo = null; | |
public $bar = null; | |
public function __get_state(){ | |
// Only show 'name' and 'bar' on my_var_export(). | |
return array('name', 'bar'); | |
} | |
} | |
$a = 'hello'; | |
my_var_export($a); | |
print "\n"; | |
$b = new Foo(); | |
$b->name = 'world'; | |
$b->foo = 'foo is foo'; | |
$b->bar = 'bar is bar'; | |
my_var_export($b); | |
print "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment