PHP: get_class_vars - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

get_class get_class_methods

Last updated: Sat, 27 Jan 2007

view this page in

# get_class_vars

(PHP 4, PHP 5)

get_class_vars -- Возвращает массив свойств класса с их инициализационными значенями

## Описание

array get_class_vars ( string class_name )

Функция возвращает ассоциативный массив свойств класса и их значения по-умолчанию (инициализационные значения). Массив имеет формат `свойство => инициализационное значение`.

> Замечание: До PHP 4.2.0 неинициализируемые переменные класса не будут возвращены функцией get_class_vars().

Пример 1. Пример использования get_class_vars()

`<?php
class myclass {
var $var1; // переменная не имеет начального значения...
var $var2 = "xyz";
var $var3 = 100;
// конструктор
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";
}
?>`

выведет:

    // Вплоть до PHP 4.2.0
    var2 : xyz
    var3 : 100

    // Начиная с PHP 4.2.0
    var1 :
    var2 : xyz
    var3 : 100

См. также get_class_methods(), get_object_vars()

add a note User Contributed Notes
get_class_vars
bernd at tiggerswelt dot net
29-Jan-2007 02:52
`If you assign a constant value using the self-scope by default to a variable, get_class_vars() will result in a FATAL error.
Example:
<?PHP
class Foo {
const Bar = "error";
public $Foo = self::Bar;
}
print_r(get_class_vars("Foo"));
?>
... but using "Foo::Bar" instead "self::Bar" will work ;)`
gizmobits at hotmail dot com
03-Mar-2006 07:48
`I wanted a simple ToString() function that was automatic and class independent. I wanted to dump it into any of several classes and get values quickly. I wanted to leave it there so I could customize it for each class, so an outside function wasn't suitable. I came up with this and thought it might be useful. Have fun!
<?php
function ToString () {
$s = "";
$s .= "<table>\n";
$s .= "<tr><td colspan=2><hr></td></tr>\n";
foreach (get_class_vars(get_class($this)) as $name => $value) {
$s .= "<tr><td>$name:</td><td>" . $this->$name . "</td></tr>\n";
}
$s .= "<tr><td colspan=2><hr></td></tr>\n";
$s .= "</table>\n";
return $s;
}
?>`
php dot net at sharpdreams dot com
25-Oct-2005 06:25
`Contrary to multiple comments throughout the manual, get_class_vars() performed within a class can access any public, protected, and private members.
<?php
class Foo {
public $x;
protected $y;
private $z;
public function __sleep() {
return( get_class_vars( __CLASS__ ) );
}
}
?>
works fine (returns x, y, & z). However, given the same class as above,
<?php
print_r( get_class_vars( "Foo" ) );
?>
will NOT return x, y, & z. Instead it will only return the public members (in our case, z).`
alan_k at php dot net
21-Jan-2005 07:23
`in PHP5 to get all the vars (including private etc.) use:
$reflection = new ReflectionClass($class);
$defaults = $reflection->getdefaultProperties();`
rec at NOSPAM dot instantmassacre dot com
23-Jan-2003 04:23
`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`
add a note
get_class get_class_methods

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST