PHP 8.3.4 Released!

is_object

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

is_objectComprueba si una variable es un objeto

Descripción

is_object(mixed $var): bool

Comprueba si la variable dada es un objeto.

Parámetros

var

La variable a ser evaluada.

Valores devueltos

Devuelve true si var es un object, o false de lo contrario.

Ejemplos

Ejemplo #1 Ejemplo is_object()

<?php
// Declarar una simple función para devolver un
// array de nuestro objeto
function get_students($obj)
{
if(!
is_object($obj)) {
return
false;
}

return
$obj->students;
}

// Declara una nueva instancia de clase y rellena
// algunos valores
$obj = new stdClass();
$obj->students = array('Kalle', 'Ross', 'Felipe');

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

Historial de cambios

Versión Descripción
7.2.0 is_object() ahora devuelve true para objetos no seriados sin una definición de clase (clase de __PHP_Incomplete_Class). Anteriormente false era devuelto.

Ver también

  • is_bool() - Comprueba si una variable es de tipo booleano
  • is_int() - Comprueba si el tipo de una variable es integer
  • is_float() - Comprueba si el tipo de una variable es float
  • is_string() - Comprueba si una variable es de tipo string
  • is_array() - Comprueba si una variable es un array

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