note 94841 deleted from function.get-class-vars by danbrown
| From: | danbrown@php.net | Date: | Fri, 27 Nov 2009 20:30:00 +0000 |
| Subject: | note 94841 deleted from function.get-class-vars by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163351@lists.php.net to get a copy of this message | ||
Note Submitter: php_getclassvars_27112009 at uchange dot co dot uk
----
Similar to gizmobits at hotmail dot com I needed a simple Dump() / ToString() function which I could
include into any class and display a list of all member variables and their values.
My solution is as follows. Note that the default separator is a newline, though you can specify
your own (i.e. <br/>) by specifying it as parameter. This makes it much easier to view the
dumped output and use it for debugging - this is one of my pet hates of print_r which always uses \n
and requires you to view the page source in order to make the values readable.
Note that this function doesn't support array values - though it should be a simple matter to
add if(is_array($this->$key)... inside the foreach loop and iterate each array element.
function Dump($sep="\n")
{
$vars = get_class_vars(get_class($this));
foreach($vars as $key=>$val)
{
printf("%s => %s%s", $key, $this->$key, $sep);
}
}
Hope this helps.