Bug #79373 [NEW]: uninitialized typed property cannot accessed in __get magic method

From: Date: Thu, 12 Mar 2020 16:56:29 +0000
Subject: Bug #79373 [NEW]: uninitialized typed property cannot accessed in __get magic method
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226069@lists.php.net to get a copy of this message
From:             consatan at gmail dot com
Operating system: Linux
PHP version:      7.4.3
Package:          Class/Object related
Bug Type:         Bug
Bug description:uninitialized typed property cannot accessed in __get magic method

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 bug report at https://bugs.php.net/bug.php?id=79373&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=79373&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=79373&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=79373&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=79373&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=79373&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=79373&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=79373&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=79373&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=79373&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=79373&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79373&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=79373&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=79373&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=79373&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=79373&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=79373&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=79373&r=mysqlcfg


Thread (4 messages)

« previous php.bugs (#226069) next »