note 103603 modified in function.isset by thiago
| From: | thiago@php.net | Date: | Mon, 23 May 2011 14:12:13 +0000 |
| Subject: | note 103603 modified in function.isset by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179961@lists.php.net to get a copy of this message | ||
isset() returns TRUE if a value is NULL. That seems wrong to me as there is no way to distinguish
between a value set to NULL and a truly undefined value.
If you have this problem inside a class, there is a fix:
<?php
class T
{
function __isset($att)
{
$props = get_object_vars($this);
return array_key_exists($att, $props);
}
}
$x = new T();
$x->foo_exists = 4;
var_dump(isset($x->foo_exists)); // TRUE
var_dump(isset($x->bar_exists)); // FALSE
?>
[EDITOR thiago NOTE: This snippet has improvements by "Paul Lashbrook"]
--was--
isset() returns TRUE if a value is NULL. That seems wrong to me as there is no way to distinguish
between a value set to NULL and a truly undefined value.
If you have this problem inside a class, there is a fix:
class T
{
function has_attribute($att)
{
$props = get_object_vars($this);
return array_key_exists($att, $props);
}
}
$x = new T();
$x->foo_exists = 4;
var_dump($x->has_attribute('foo_exists')); // TRUE
var_dump($x->has_attribute('bar_exists')); // FALSE
http://php.net/manual/en/function.isset.php