On 26/05/2016 11:49, Fleshgrinder wrote:
On 5/26/2016 12:39 PM, Rowan Collins wrote:
On 26/05/2016 11:16, Fleshgrinder wrote:
  $o = (object) ['x' => null];
  var_dump(isset($a->x));              // false
  var_dump(property_exists($a, 'x'));  // true

Apart from a typo in your example (you change from $o to $a), this is
already the current behaviour, and always has been: https://3v4l.org/NeqGl

isset() is really very simple: if the thing your accessing would give
you the value null, it returns false, otherwise it returns true.

Regards,

Now I feel stupid but I guess I got lost myself. :P

Heh, no worries, easily done. :)


This means it is even simpler, we just have to add the E_NOTICE and be
done with it.

  $g = new class { public $x; };

  var_dump($g->x);                     // null + E_NOTICE Uninitialized
  var_dump(isset($g->x));              // false
  var_dump(property_exists($g, 'x'));  // true
  var_dump(is_null($g->x));            // true + E_NOTICE Uninitialized
  var_dump($g->x == null);             // true + E_NOTICE Uninitialized
  var_dump($g->x === null);            // true + E_NOTICE Uninitialized

This behavior would be true for all variations:

  class A {

    var $x;

    public $x;

    public int $x;

    public ?int $x;

  }

No notice for the following:

  class A {

    var $x = null;

    public $x = null;

    public int $x = 0;

    public ?int $x = null;

  }



I am definitely in favour of this from a language point of view. The notice is warning about *initialization*, not *declaration*, so it makes sense to warn on "var $foo" and not "var $foo = null".

The only thing I'm not sure is what the impact would have on the internals, which may be why it doesn't already work that way. With a simple variable the notice can be easily raised based on the variable name not being in the current symbol table; with an object property, the name *is* in the relevant table, because it's been *declared* even though it hasn't been *initialized*. Thus you need some extra flag to track that a declared property exists but has never been written to.

But it sounds like typed properties require adding some such overhead anyway...

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to