Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111847 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72370 invoked from network); 4 Sep 2020 19:20:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Sep 2020 19:20:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3856F180541 for ; Fri, 4 Sep 2020 11:25:55 -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:25:53 -0700 (PDT) Received: from 70ad81a75ce4 (10.228.0.201) by mail-mahalux.mvorisek.com (10.228.0.4) with Microsoft SMTP Server (TLS); Fri, 4 Sep 2020 20:25:50 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_fa3cfbe976918e11a95035b7a87d41a0" Date: Fri, 04 Sep 2020 20:25:49 +0200 To: David Rodrigues Cc: PHP internals In-Reply-To: References: <931ab9a3fbe85bd5c9cb083dbf0611f7ea1896fa7ddd2bce3a7e46a0630c48b6@mahalux.com> Message-ID: 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?=) --=_fa3cfbe976918e11a95035b7a87d41a0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8; format=flowed Yes, this is purely to offer native/php support for it. When implementing custom ORM etc., we do not have any control over the class, ie. extra method for checking property existance is not possible. With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem, Michael Voříšek, student On 4 Sep 2020 20:14, David Rodrigues wrote: > Maybe you just can implements your own method to check? Like your exists() > example. > > Em sex, 4 de set de 2020 15:08, Michael Voříšek - ČVUT FEL < > vorismi3@fel.cvut.cz> escreveu: > > 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 < vorismi3@fel.cvut.cz> 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/ --=_fa3cfbe976918e11a95035b7a87d41a0--