note 94841 added to function.get-class-vars
| From: | php_getclassvars_27112009 at uchange dot co dot uk | Date: | Fri, 27 Nov 2009 20:16:31 +0000 |
| Subject: | note 94841 added to function.get-class-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163349@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 87.124.86.47
Probable Submitter: 94.195.160.63 (proxied: 94.195.160.63)
----
Manual Page -- http://www.php.net/manual/en/function.get-class-vars.php
Edit -- https://master.php.net/note/edit/94841
Del: integrated -- https://master.php.net/note/delete/94841/integrated
Del: useless -- https://master.php.net/note/delete/94841/useless
Del: bad code -- https://master.php.net/note/delete/94841/bad+code
Del: spam -- https://master.php.net/note/delete/94841/spam
Del: non-english -- https://master.php.net/note/delete/94841/non-english
Del: in docs -- https://master.php.net/note/delete/94841/in+docs
Del: other reasons-- https://master.php.net/note/delete/94841
Reject -- https://master.php.net/note/reject/94841
Search -- https://master.php.net/manage/user-notes.php