Bug #79373 [Opn->Nab]: uninitialized typed property cannot accessed in __get magic method
Edit report at https://bugs.php.net/bug.php?id=79373&edit=1
ID: 79373
Updated by: beberlei@php.net
Reported by: consatan at gmail dot com
Summary: uninitialized typed property cannot accessed in
__get magic method
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Linux
PHP Version: 7.4.3
Block user comment: N
Private report: N
New Comment:
Your example seems to be wrong, you are not accessing Foo::$bar through __get, because its public. I
assume its an error while making the example small and contained.
However the behavior here is correct regardless of __get or public access. You cannot access an
uninitialized typed variable anywhere, before initializing it and this error represents that.
Previous Comments:
------------------------------------------------------------------------
[2020-03-12 16:56:29] consatan at gmail dot com
Description:
------------
A uninitialized typed property cannot accessed in __get method until unset() it. Test on
7.4.0~7.4.3, 7.4.0 is works.
Test script:
---------------
<?php
class Foo {
public int $bar;
public function __get($name) {
if ('bar' === $name) {
return $this->bar = 1;
}
}
}
$foo1 = new Foo();
var_dump($foo1);
var_dump($foo1->bar);
$foo2 = new Foo();
var_dump($foo2);
unset($foo2->bar);
var_dump($foo2);
var_dump($foo2->bar);
Expected result:
----------------
object(Foo)#1 (0) {
["bar"]=>
uninitialized(int)
}
int(1)
object(Foo)#2 (0) {
["bar"]=>
uninitialized(int)
}
object(Foo)#2 (0) {
["bar"]=>
uninitialized(int)
}
int(1)
Actual result:
--------------
object(Foo)#1 (0) {
["bar"]=>
uninitialized(int)
}
Warning: Uncaught Error: Typed property Foo::$bar must not be accessed before initialization in php
shell code:1
Stack trace:
#0 {main}
thrown in php shell code on line 1
object(Foo)#2 (0) {
["bar"]=>
uninitialized(int)
}
object(Foo)#2 (0) {
["bar"]=>
uninitialized(int)
}
int(1)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79373&edit=1
Thread (4 messages)