Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111846 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70270 invoked from network); 4 Sep 2020 19:09:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Sep 2020 19:09:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D402818053B for ; Fri, 4 Sep 2020 11:14:31 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, MISSING_HEADERS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 4 Sep 2020 11:14:31 -0700 (PDT) Received: by mail-ot1-f54.google.com with SMTP id i4so6718654ota.2 for ; Fri, 04 Sep 2020 11:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:cc; bh=KnEflA0HGA3A12mOyZ+6euQ10TMzZaunW7nifZSp7vM=; b=jh476e01YYIBTf2Nthk0xX1DxbMOvYng2K257VPoVC6rP65b6nEerME48q5zgL4kf4 WNkEkIuQy+t7mYVaRI/7PIVMOm5ZviANQOY+64n2RnsAtGTz8DLJuZalkrvrhTOFv10E JVIbBsvPsss3RU+hpWZfZHuvHQGXfWF3V5dbhNQNcJtscxwDxZQHdOEZvrzfDMxgPx5H 59sYFjH9q230oCULoRj+7vTs+6XW317WPfap6vI8jz+3C9J8VdAb/g9DUIjlR+r2N5qv ttDBRV8q38sb/shy0Rav7Qmgqgp4h8TFxyMM6eZ8z4dkHkEIKby73DyM54GQQ2fXEFvp 8liw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:cc; bh=KnEflA0HGA3A12mOyZ+6euQ10TMzZaunW7nifZSp7vM=; b=SxVlGi4hPpTm3iCm5KsjtMDPJFXE0Nyg9STsWDy6ZADL2EjpeKPlUjybLU1xj+oUAA URNl83v+ZEiXtYaBB2acobz3OzsSiv6/LezdlDHptGaj48SxE4QbtkOG0oh8YQ0vxJ5V gru+J80ONwzatFgEjkvXEbHXtUfOi0C5ODZ4zYdDZYdKidZGcb6c62TlnK26TRyV8V0l 4oFqiUqjdS4E2EXUtzP/IlxbG6AsGUj82IIZf8fo/VM8u19dG4yaXkWnbSSS/cFi8ZcA HADFAOmnsz09D5HlanDuNSOWHnndaBZv9uPOr7dzW54TqjhIXzse8A9TYSFnw2CKE0Ph caug== X-Gm-Message-State: AOAM531yMKGE7uU8J7wywxFPJCmOSrSpk7SsVCTovFYol7ab/YTs83nu EX1uID/enLUk+/GmmooEKS8V+eQSeeyD6O21BzCl03ug X-Google-Smtp-Source: ABdhPJzA2bHCj/75JMZGmJnokgtU9cDIagQu8EqnoMrZ0xyLmISsTd/CoFjxTrxlj0TckCIcAwg6FXGT/f2YmtBEBog= X-Received: by 2002:a9d:7745:: with SMTP id t5mr6329397otl.114.1599243267990; Fri, 04 Sep 2020 11:14:27 -0700 (PDT) MIME-Version: 1.0 References: <931ab9a3fbe85bd5c9cb083dbf0611f7ea1896fa7ddd2bce3a7e46a0630c48b6@mahalux.com> In-Reply-To: <931ab9a3fbe85bd5c9cb083dbf0611f7ea1896fa7ddd2bce3a7e46a0630c48b6@mahalux.com> Date: Fri, 4 Sep 2020 15:14:12 -0300 Message-ID: Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000e5e64705ae80d6cb" Subject: Re: [PHP-DEV] __isset() and null value From: david.proweb@gmail.com (David Rodrigues) --000000000000e5e64705ae80d6cb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=C5=99=C3=AD=C5=A1ek - =C4=8CVUT = 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=C3=BC=C3=9Fen / S p=C5=99=C3=A1te= lsk=C3=BDm pozdravem, > > Michael Vo=C5=99=C3=AD=C5=A1ek > > On 4 Sep 2020 19:23, Marco Pivetta wrote: > > > Heya, > > > > On Fri, Sep 4, 2020 at 7:03 PM Michael Vo=C5=99=C3=AD=C5=A1ek - =C4=8CV= UT 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 i= f > >> value is null and for BC autoimplement __isset() based on it and __get= , > >> ie. > >> > >> function __isset($n) { return $this->__exist($n) && $this->__isset($n) > >> !=3D=3D 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 stati= c > 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 =3D null; > > private $bar =3D 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 =3D null; > > private $bar =3D 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/ --000000000000e5e64705ae80d6cb--