get_class_vars
get_class_vars
(PHP 4, PHP 5, PHP 7)
get_class_vars - 获取类的默认属性
描述
array get_class_vars ( string $class_name )
获取给定类的默认属性。
参数
class_name
类名称
返回值
返回从当前作用域可见的已声明属性的关联数组,其默认值为。结果数组元素的形式为varname => value。如果出现错误,则返回FALSE。
更新日志
Version | Description |
---|---|
5.0.3 | 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. |
例子
示例#1 get_class_vars()示例
<?php
class myclass {
var $var1; // this has no default value...
var $var2 = "xyz";
var $var3 = 100;
private $var4;
// constructor
function __construct() {
// 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";
}
?>
上面的例子将输出:
var1 :
var2 : xyz
var3 : 100
示例#2 get_class_vars()和范围行为
<?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')
?>
上面的例子将输出:
// 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
可以参阅
- get_class_methods() - 获取类方法的名称
- get_object_vars() - 获取给定对象的属性
← get_class_methods
get_class →