note 122960 deleted from language.operators.comparison by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 17:47:12 +0000 |
| Subject: | note 122960 deleted from language.operators.comparison by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217879@lists.php.net to get a copy of this message | ||
Note Submitter: Romain
----
?? vs isset() with __isset() and __get()
<?php
class A
{
public function __get($id)
{
return 'a value';
}
}
class B
{
public function __isset($id)
{
return false;
}
public function __get($id)
{
return 'a value';
}
}
class c
{
protected $test = "a value";
public function __isset($id)
{
return true;
}
}
$a = new A();
$b = new B();
$c = new C();
echo isset($a->test) ? $a->test : 'nothing'; // "nothing"
echo $a->test ?? 'nothing'; // "a value", isset() is not called!
echo isset($b->test) ? $b->test : 'nothing'; // "nothing"
echo $b->test ?? 'nothing'; // "nothing", __isset() is called
echo isset($c->test) ? $c->test : 'nothing'; // throw an Exception
echo $c->test ?? 'nothing'; // "nothing"
?>