I know you can do this
class Object
{
private $ar;
public function __isset($name)
{
return isset($this->ar[$name]);
}
}
which can then be used to do the following
$obj = new Object();
if (isset($obj->name)) { /* ... */ }
However is there a way to do this
$obj = new Object();
if (isset($obj)) { /* .... */ }
Where i can control the return of $obj
status using the __isset()
magic method on the object it self.
You could only define a new global function myIsset()
or something like it to do this.
function myIsset($obj = NULL)
{
...
}
When checking the variable $obj
with isset
PHP doesn't interact with the object that might be referenced by the variable at all.
You cannot, because it would not make any sense (at least not in the way isset()
is meant to be used). So isset($obj)
is always true as long as it points to some object and not NULL/undefined.
Magic method __isset
is not intended to be used that way.
According to PHP manual:
"__isset() is triggered by calling isset() or empty() on inaccessible properties."