note 109995 added to function.get-class-vars
| From: | bof at bof dot de | Date: | Sun, 09 Sep 2012 07:02:07 +0000 |
| Subject: | note 109995 added to function.get-class-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190591@lists.php.net to get a copy of this message | ||
I needed to get only the class static variables, leaving out instance variables.
<?php
function get_static_vars($class) {
$result = array();
foreach (get_class_vars($class) as $name => $default)
if (isset($class::$$name))
$result[$name] = $default;
return $result;
}
?>
That function returns only the public ones. The same pattern can be used inside a class, then it
returns private and protected static variables, too:
<?php
static protected function get_static_vars($class = NULL) {
if (!isset($class)) $class = get_called_class();
$result = array();
foreach (get_class_vars($class) as $name => $default)
if (isset($class::$$name))
$result[$name] = $default;
return $result;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 213.135.15.139
----
Manual Page -- http://www.php.net/manual/en/function.get-class-vars.php
Edit -- https://master.php.net/note/edit/109995
Del: integrated -- https://master.php.net/note/delete/109995/integrated
Del: useless -- https://master.php.net/note/delete/109995/useless
Del: bad code -- https://master.php.net/note/delete/109995/bad+code
Del: spam -- https://master.php.net/note/delete/109995/spam
Del: non-english -- https://master.php.net/note/delete/109995/non-english
Del: in docs -- https://master.php.net/note/delete/109995/in+docs
Del: other reasons-- https://master.php.net/note/delete/109995
Reject -- https://master.php.net/note/reject/109995
Search -- https://master.php.net/manage/user-notes.php