PHP 8.3.4 Released!

is_object

(PHP 4, PHP 5, PHP 7, PHP 8)

is_object查找变量是否是对象

说明

is_object(mixed $value): bool

查找指定变量是否是对象。

参数

value

要计算的变量。

返回值

如果 valueobject,返回 true,否则返回 false

更新日志

版本 说明
7.2.0 没有定义类(__PHP_Incomplete_Class 类)的未序列化对象,is_object() 现在返回 true,之前返回 false

示例

示例 #1 is_object() 示例

<?php
// 声明简单的函数,从对象中返回数组
function get_students($obj)
{
if (!
is_object($obj)) {
return
false;
}

return
$obj->students;
}

// 声明新的类实例并填充值
$obj = new stdClass();
$obj->students = array('Kalle', 'Ross', 'Felipe');

var_dump(get_students(null));
var_dump(get_students($obj));
?>

参见

add a note

User Contributed Notes 2 notes

up
94
peter dot nagel at portavita dot nl
12 years ago
Note: is_object(null) returns false

This should actually be part of the input/output specification at the top of this page.
up
5
mark at not4you dot com
12 years ago
Unserializes data as returned by the standard PHP serialize() function. If the unserialized object is not an array, it will be converted to one, particularily useful if it returns a __PHP_Incomplete_Class.

<?php
/**
*
* @param string $data Serialized data
*
* @return array Unserialized array
*/
function unserialize2array($data) {
$obj = unserialize($data);
if(
is_array($obj)) return $obj;
$arr = array();
foreach(
$obj as $k=>$v) {
$arr[$k] = $v;
}
unset(
$arr['__PHP_Incomplete_Class_Name']);
return
$arr;
}
?>
To Top