Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111845 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68614 invoked from network); 4 Sep 2020 19:02:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Sep 2020 19:02:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6E60518058D for ; Fri, 4 Sep 2020 11:08:24 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,HTML_MESSAGE, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-mahalux.mvorisek.com (mail-mahalux.mvorisek.com [77.93.195.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 4 Sep 2020 11:08:22 -0700 (PDT) Received: from 4c40f40fcd1b (10.228.0.243) by mail-mahalux.mvorisek.com (10.228.0.4) with Microsoft SMTP Server (TLS); Fri, 4 Sep 2020 20:07:58 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_452929fa89b50ae5abd1d723f23a40fa" Date: Fri, 04 Sep 2020 20:07:57 +0200 To: Marco Pivetta Cc: PHP internals In-Reply-To: References: Message-ID: <931ab9a3fbe85bd5c9cb083dbf0611f7ea1896fa7ddd2bce3a7e46a0630c48b6@mahalux.com> X-Mailer: SAP NetWeaver 7.03 Subject: Re: [PHP-DEV] __isset() and null value From: vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=) --=_452929fa89b50ae5abd1d723f23a40fa Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8; format=flowed Your examples provide code for checking the existance of real properties. But how to check existance of a magic one? The best is currently __isset(), but to comply with isset() definition, it should not return true when the magic property has null value, thus I belive, there is currently not way (provided by php language) to check for existance of magic property. With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem, Michael Voříšek On 4 Sep 2020 19:23, Marco Pivetta wrote: > Heya, > > On Fri, Sep 4, 2020 at 7:03 PM Michael Voříšek - ČVUT FEL wrote: > >> isset() returns false for null >> >> __isset() should return the same, but then if magic property with null >> value exists, there is no way to detect it >> >> Example: https://3v4l.org/GqUsh >> >> this is currently an limitation of php >> >> Ideally, we should introduce __exist() which should return true even if >> value is null and for BC autoimplement __isset() based on it and __get, >> ie. >> >> function __isset($n) { return $this->__exist($n) && $this->__isset($n) >> !== null; } > > I'd endorse **NOT** checking for property existence on objects that don't have a clearly defined interface/type: that's something for a static analyzer, not (usually) for runtime code. > > If you still need to do that (anti-patterns such as stuffing things in `stdClass` instances), checking if a property is defined is trivial with reflection: > > ```php > > class SomethingMagicAndTerriblyUgly > { > public $foo = null; > private $bar = null; > } > > var_dump((new ReflectionClass(SomethingMagicAndTerriblyUgly::class))->hasProperty('foo')); > var_dump((new ReflectionClass(SomethingMagicAndTerriblyUgly::class))->hasProperty('bar')); > var_dump((new ReflectionClass(SomethingMagicAndTerriblyUgly::class))->hasProperty('baz')); > ``` > > https://3v4l.org/pVC4j > > Checking if a **public** property exists at runtime is done via `array_key_exists()`, not via `isset()`: > > ```php > > class SomethingMagicAndTerriblyUgly > { > public $foo = null; > private $bar = null; > } > > var_dump(array_key_exists('foo', (array) (new SomethingMagicAndTerriblyUgly))); > var_dump(array_key_exists('bar', (array) (new SomethingMagicAndTerriblyUgly))); > ``` > > https://3v4l.org/ZLSjq > > Marco Pivetta > > http://twitter.com/Ocramius > > http://ocramius.github.com/ --=_452929fa89b50ae5abd1d723f23a40fa--