PHP: is_object - Manual (original) (raw)
(PHP 4, PHP 5, PHP 7, PHP 8)
is_object — Finds whether a variable is an object
Description
Parameters
value
The variable being evaluated.
Changelog
| Version | Description |
|---|---|
| 7.2.0 | is_object() now returns true for unserialized objects without a class definition (class of __PHP_Incomplete_Class). Previously false was returned. |
Examples
Example #1 is_object() example
`<?php
// 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));
?>`
See Also
- is_bool() - Finds out whether a variable is a boolean
- is_int() - Find whether the type of a variable is integer
- is_float() - Finds whether the type of a variable is float
- is_string() - Find whether the type of a variable is string
- is_array() - Finds whether a variable is an array
Found A Problem?
peter dot nagel at portavita dot nl ¶
14 years ago
Note: is_object(null) returns false
This should actually be part of the input/output specification at the top of this page.
14 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) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>o</mi><mi>b</mi><mi>j</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>s</mi><mi>e</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>l</mi><mi>i</mi><mi>z</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">obj = unserialize(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.05724em;">bj</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mord mathnormal">ia</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord mathnormal">ze</span><span class="mopen">(</span></span></span></span>data);
if(is_array($obj)) return $obj;
$arr = array();
foreach($obj as <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mo>=</mo><mo>></mo></mrow><annotation encoding="application/x-tex">k=></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7335em;vertical-align:-0.0391em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=></span></span></span></span>v) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">arr[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mopen">[</span></span></span></span>k] = $v;
}
unset($arr['__PHP_Incomplete_Class_Name']);
return $arr;
}
?>