CakeFest 2024: The Official CakePHP Conference

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));
?>

参考

  • is_bool() - 変数が boolean であるかを調べる
  • is_int() - 変数が整数型かどうかを検査する
  • is_float() - 変数の型が float かどうか調べる
  • is_string() - 変数の型が文字列かどうかを調べる
  • is_array() - 変数が配列かどうかを検査する

add a note

User Contributed Notes 2 notes

up
94
peter dot nagel at portavita dot nl
13 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