Check if a variable is an object in PHP

Description

The following code shows how to check if a variable is an object.

Example


<?php// www  .  j  a  v a 2  s  .  com
//Declare a simple function to return an array from our object
function get_students($obj){
    if (!is_object($obj)) {
        return false;
    }
    return $obj->students;
}

// Declare a new class instance and fill up
// some values
$obj = new stdClass();
$obj->students = array('Kalle', 'Ross', 'Felipe');

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

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition