__isset() and null value

  111843
September 4, 2020 17:03 vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=)
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; } 

With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem,

Michael Voříšek
  111844
September 4, 2020 17:23 ocramius@gmail.com (Marco Pivetta)
Heya,

<http://ocramius.github.com/>


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 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 https://3v4l.org/ZLSjq Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/
  111845
September 4, 2020 18:07 vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=)
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/
  111846
September 4, 2020 18:14 david.proweb@gmail.com (David Rodrigues)
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/
  111847
September 4, 2020 18:25 vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=)
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)));
  111848
September 5, 2020 20:44 rowan.collins@gmail.com (Rowan Tommins)
Hi Michael,

On 4 September 2020 18:03:29 BST, "Michael Voříšek - ČVUT FEL" <vorismi3@fel.cvut.cz> wrote:
>Ideally, we should introduce __exist() which should return true even if >value is null
Magic methods in PHP allow the user to overload some built-in operator or behaviour, so it doesn't make sense to talk about a new magic method in terms of how users are expected to implement it without first describing what built-in functionality it overloads. Is the idea that $foo->__exists($bar) would be called when running property_exists($foo, $bar)? If so I suspect most cases would be clearer if they used some context-specific interface with a well-named method. For instance: interface SomeORM\DynamicDBModel { public function hasColumn($columnName): bool; } A notable difference from __isset is that you can write isset($foo->bar) without having 'bar' as a string value, which AFAIK is not possible with "exists". Regards, -- Rowan Tommins [IMSoP]