Bug #80037 [NEW]: Typed property must not be accessed before initialization when __get() declared

From: Date: Mon, 31 Aug 2020 08:43:18 +0000
Subject: Bug #80037 [NEW]: Typed property must not be accessed before initialization when __get() declared
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228806@lists.php.net to get a copy of this message
From:             miloslav dot hula at gmail dot com
Operating system: 
PHP version:      7.4.9
Package:          *General Issues
Bug Type:         Bug
Bug description:Typed property must not be accessed before initialization when __get() declared

Description:
------------
Test script leads to fatal error when magic method __get() is declared
and assign by reference.

Test script:
---------------
<?php

final class A
{
	public string $a;

	public static function fromArray(array $props): self
	{
		$me = new static;
		foreach ($props as $k => &$v) {
			$me->{$k} = &$v;  # try to remove &
		}
		return $me;
	}

	public function __get($name)
	{
		throw new \LogicException("Property '$name' is not defined.");
	}
}

A::fromArray(['a' => 'foo']);

Expected result:
----------------
(no error)

Actual result:
--------------
Fatal error: Uncaught Error: Typed property A::$a must not be accessed
before initialization.

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


Thread (4 messages)

« previous php.bugs (#228806) next »