Bug #79192 [Opn]: unset does not unset uninitialized typed properties completely
| From: | raincomplain at outlook dot com | Date: | Thu, 30 Jan 2020 09:05:12 +0000 |
| Subject: | Bug #79192 [Opn]: unset does not unset uninitialized typed properties completely | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225240@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79192&edit=1
ID: 79192
User updated by: raincomplain at outlook dot com
Reported by: raincomplain at outlook dot com
Summary: unset does not unset uninitialized typed properties
completely
Status: Open
Type: Bug
Package: Class/Object related
Operating System: Linux
PHP Version: 7.4.2
Block user comment: N
Private report: N
New Comment:
I would also add that if a property accepts null value
public ?int $bar;
Then unsetting it should work even if it's not initialized, since unsetting it would set it to
null.
But what we saw here is totally unexpected behavior. If unsetting it is not allowed then why magic
methods are triggered?
Previous Comments:
------------------------------------------------------------------------
[2020-01-29 17:32:35] raincomplain at outlook dot com
Changing bug package
------------------------------------------------------------------------
[2020-01-29 17:12:29] raincomplain at outlook dot com
Description:
------------
After unsetting an uninitialized typed property any attempt to modify that property will still
generate a fatal error an unset seems to set that property to null which is another issue. The later
can be illustrated after adding the magic method __get.
Test script:
---------------
**Without __get**
<?php
class Foo
{
public int $bar;
public function __set($prop, $val) {
throw new Exception('Dynamic properties are not allowed');
}
}
$f = new Foo();
unset($f->bar);
echo $f->bar
?>
**With __get**
class Foo
{
public int $bar;
public function __set($prop, $val) {
throw new Exception('Dynamic properties are not allowed');
}
public function __get($name)
{
echo 'works'.PHP_EOL;
}
}
$f = new Foo();
unset($f->bar);
echo $f->bar
Expected result:
----------------
The expected behavior is to emit a notice (PHP Notice: Undefined property: Foo::$bar) or prevent
unsetting uninitialized typed properties in the first place.
Actual result:
--------------
**Without __get**
Typed property Foo::$bar must not be accessed before initialization
**With __get**
works
PHP Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, null used
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79192&edit=1