get_object_vars 返回由对象属性组成的关联数组
发表日期:2021-07-01 08:57:11 | 来源: | | 浏览(1200) 分类:类/对象 函数
get_object_vars
(PHP 4, PHP 5, PHP 7, PHP 8)
get_object_vars — 返回由对象属性组成的关联数组
说明
get_object_vars(object
$obj): array
返回由 obj
指定的对象中定义的属性组成的关联数组。
注意:
在 PHP 4.2.0 之前的版本中,如果在
obj对象实例中声明的变量没有被赋值,则它们将不会在返回的数组中。而在 PHP 4.2.0 之后,这些变量作为键名将被赋予null值。
示例 #1 使用 get_object_vars()
<?php
class Point2D {
var $x, $y;
var $label;
function Point2D($x, $y) {
$this->x = $x;
$this->y = $y;
}
function setLabel($label) {
$this->label = $label;
}
function getPoint() {
return array("x" => $this->x, "y" => $this->y, "label" => $this->label);
}
}
// "$label" is declared but not defined$p1 = new Point2D(1.233, 3.445);
print_r(get_object_vars($p1));
$p1->setLabel("point #1");
print_r(get_object_vars($p1));
?>
以上例程会输出:
Array
(
[x] => 1.233
[y] => 3.445
[label] =>
)
Array
(
[x] => 1.233
[y] => 3.445
[label] => point #1
)
参数
-
object -
An object instance.
返回值
Returns an associative array of defined object accessible non-static properties
for the specified object in scope. If a property have
not been assigned a value, it will be returned with a null value.
更新日志
| 版本 | 说明 |
|---|---|
| 5.3.0 |
This function now returns NULL if the
object isn't an object.
|
| prior to 5.3.0 |
If the object isn't an object, then
get_object_vars() would return false
|
| prior to 4.2.0 |
If the variables declared in the class of which the
object is an instance, have not been assigned a
value, those will not be returned in the array
|
范例
示例 #2 Use of get_object_vars()
<?php
class foo {
private $a;
public $b = 1;
public $c;
private $d;
static $e;
public function test() {
var_dump(get_object_vars($this));
}
}
$test = new foo;
var_dump(get_object_vars($test));
$test->test();
?>
以上例程会输出:
array(2) {
["b"]=>
int(1)
["c"]=>
NULL
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
NULL
["d"]=>
NULL
}
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- __autoload 尝试加载未定义的类(0)
- class_alias 为一个类创建别名(0)
- class_exists 检查类是否已定义(0)
- get_called_class 后期静态绑定("Late Static Binding")类的名称(0)
- get_class_methods 返回由类的方法名组成的数组(0)
- get_class_vars 返回由类的默认属性组成的数组(0)
- get_class 返回对象的类名(0)
- get_declared_classes 返回由已定义类的名字所组成的数组(0)
- get_declared_interfaces 返回一个数组包含所有已声明的接口(0)
- get_declared_traits 返回所有已定义的 traits 的数组(0)
- get_object_vars 返回由对象属性组成的关联数组(0)
- get_parent_class 返回对象或类的父类名(0)
- interface_exists 检查接口是否已被定义(0)
- is_a 如果对象属于该类或该类是此对象的父类则返回 true(0)
- is_subclass_of 如果此对象是该类的子类,则返回 true(0)
- method_exists 检查类的方法是否存在(0)
- property_exists 检查对象或类是否具有该属性(0)
- trait_exists 检查指定的 trait 是否存在(0)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号