get_class_vars
(PHP 4, PHP 5)
get_class_vars — Get the default properties of the class
Description
array get_class_vars
( string
$class_name
)Get the default properties of the given class.
Parameters
-
class_name
-
The class name
Return Values
Returns an associative array of declared properties visible from the
current scope, with their default value.
The resulting array elements are in the form of
varname => value.
In case of an error, it returns FALSE
.
Changelog
Version | Description |
---|---|
5.0.3 | Depending on the scope, get_class_vars() will only return the properties that can be accessed from the current scope. |
5.0.2 | Calling get_class_vars() will now expose all the properties as an array, unlike previous behaviour where protected and private properties were prefixed with nul bytes. |
5.0.1 | Calling get_class_vars() will expose all properties, as when converting an object to a class. |
4.2.0 | Uninitialized class variables are now also reported by get_class_vars(). |
Examples
Example #1 get_class_vars() example
<?php
class myclass {
var $var1; // this has no default value...
var $var2 = "xyz";
var $var3 = 100;
private $var4; // PHP 5
// constructor
function myclass() {
// change some properties
$this->var1 = "foo";
$this->var2 = "bar";
return true;
}
}
$my_class = new myclass();
$class_vars = get_class_vars(get_class($my_class));
foreach ($class_vars as $name => $value) {
echo "$name : $value\n";
}
?>
The above example will output:
// Before PHP 4.2.0 var2 : xyz var3 : 100 // As of PHP 4.2.0 var1 : var2 : xyz var3 : 100
Example #2 get_class_vars() and scoping behaviour
<?php
function format($array)
{
return implode('|', array_keys($array)) . "\r\n";
}
class TestCase
{
public $a = 1;
protected $b = 2;
private $c = 3;
public static function expose()
{
echo format(get_class_vars(__CLASS__));
}
}
TestCase::expose();
echo format(get_class_vars('TestCase'));
?>
The above example will output:
// 5.0.0 a| * b| TestCase c a| * b| TestCase c // 5.0.1 - 5.0.2 a|b|c a|b|c // 5.0.3 + a|b|c a
See Also
- get_class_methods() - Gets the class methods' names
- get_object_vars() - Gets the properties of the given object
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Функции работы с классами и объектами
- __autoload
- call_user_method_array
- call_user_method
- class_alias
- class_exists
- get_called_class
- get_class_methods
- get_class_vars
- get_class
- get_declared_classes
- get_declared_interfaces
- get_declared_traits
- get_object_vars
- get_parent_class
- interface_exists
- is_a
- is_subclass_of
- method_exists
- property_exists
- trait_exists
Коментарии
If you want to retrieve the class vars from within the class itself, use $this.
<?php
class Foo {
var $a;
var $b;
var $c;
var $d;
var $e;
function GetClassVars()
{
return array_keys(get_class_vars(get_class($this))); // $this
}
}
$Foo = new Foo;
$class_vars = $Foo->GetClassVars();
foreach ($class_vars as $cvar)
{
echo $cvar . "<br />\n";
}
?>
Produces, after PHP 4.2.0, the following:
a
b
c
d
e
There seems to be be a function to get constants missing , i.e. get_class_constants() ... so here is a simple function for you all. Hopefully Zend will include this in the next round as a native php call, without using reflection.
<?php
function GetClassConstants($sClassName) {
$oClass = new ReflectionClass($sClassName);
return $oClass->getConstants());
}
?>
If you need get the child protected/private vars ignoring the parent vars, use like this:
<?php
class childClass extends parentClass {
private $login;
private $password;
public function __set($key, $val) {
if ($key == 'password')
$this->$key = md5($val);
else
$this->$key = $val;
}
}
class parentClass {
public $name;
public $email;
function __construct() {
$reflection = new ReflectionClass($this);
$vars = array_keys($reflection->getdefaultProperties());
$reflection = new ReflectionClass(__CLASS__);
$parent_vars = array_keys($reflection->getdefaultProperties());
$my_child_vars = array();
foreach ($vars as $key) {
if (!in_array($key, $parent_vars)) {
$my_child_vars[] = $key;
}
}
print_r($my_child_vars);
}
}
$child_class = new childClass();
?>
All 3 of get_object_vars, get_class_vars and reflection getDefaultProperties will reveal the name of the array. For serialization I recommend:
<?php
$cName = get_class($this);
$varTemplate= get_class_vars($cName)
foreach ($varTemplate as $name => $defaultVal) {
$vars[$name] = $this->$name; // gets actual val.
}
?>
No scan the $vars and create serialization string how you wish.
This protects against erroneous prior deserializing in maintaining the integrity of the class template and ignoring unintended object properties.
I propse following for getting Public members, always:
<?PHP
if (!function_exists("get_public_class_vars")) {
function get_public_class_vars($class) {
return get_class_vars($class);
}
}
if (!function_exists("get_public_object_vars")) {
function get_public_object_vars($object) {
return get_object_vars($object);
}
}
?>
This is to mitigate the problem and a feature that get_object_vars($this) returns private members. Running it simply outside the scope will get the public.
Iterating public members only and their defaults are enormously useful in e.g. in serialization classes such as options where each public member is an serializable that is saved and loaded.
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;
}
?>
<?php
class someClass {
public function toArray() {
$records = array();
foreach( $this as $key => $value ) {
$records[$key] = $value;
}
return $records;
}
}
?>