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
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 itExample: 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'));
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)));
Marco Pivetta
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
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 itExample: 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'));
Checking if a public property exists at runtime is done via
array_key_exists()
, not viaisset()
:<?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)));
Marco Pivetta
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
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 itExample: 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 viaisset()
:<?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/
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
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
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 itExample: 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/
Hi Michael,
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]