phpday 2027 - Call for Papers

is_object

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

is_object — Verifica se una variabile è un object

Descrizione

function is_object(mixed $value): bool

Verifica se una variabile è un object.

Elenco dei parametri

value

La variabile da valutare.

Valori restituiti

Restituisce true se value è un object, false in caso contrario.

Log delle modifiche

Versione Descrizione
7.2.0 is_object() ora restituisce true per gli oggetti deserializzati senza una definizione di classe (classe __PHP_Incomplete_Class). In precedenza veniva restituito false.

Esempi

Example #1 Esempio di is_object()

<?php
// Dichiarazione di una semplice funzione che restituisce un 
// array dal nostro oggetto
function get_students($obj)
{
    if (!is_object($obj)) {
        return false;
    }

    return $obj->students;
}

// Dichiara una nuova istanza della classe e la riempie 
// con diversi valori
$obj = new stdClass();
$obj->students = array('Kalle', 'Ross', 'Felipe');

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

Vedere anche:

  • is_bool() - Dice se una variabile è un booleano
  • is_int() - Verifica se una variabile è di tipo integer
  • is_float() - Verifica se una variabile è di tipo float (decimale a virgola mobile)
  • is_string() - Verifica se il tipo di una variabile sia stringa
  • is_array() - Verifica se una variabile è un array

+add a note

User Contributed Notes 3 notes

up
101
peter dot nagel at portavita dot nl
15 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
4
mark at not4you dot com
15 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;
}
?>
up
2
monique at nijboer dot it
2 years ago
I would expect a reference to is_a() function here.
if you would test if an object is of an specific type use is_a()
https://www.php.net/manual/en/function.is-a.php
To Top