|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80037 Typed property must not be accessed before initialization when __get() declared
Submitted: 2020-08-31 08:43 UTC Modified: 2020-08-31 10:14 UTC
From: miloslav dot hula at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 7.4.9 OS:
Private report: No CVE-ID: None
 [2020-08-31 08:43 UTC] miloslav dot hula at gmail dot com
Test script leads to fatal error when magic method __get() is declared and assign by reference.

Test script:

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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-31 09:51 UTC]
Interesting case. Might actually be a bug, despite the __get + by-ref assignment combination. After all, uninitialized typed properties are not supposed to trigger magic methods, so the existence of __get should be immaterial here...
 [2020-08-31 10:14 UTC]
-Assigned To: +Assigned To: nikic
 [2020-08-31 10:19 UTC]
Automatic comment on behalf of
Log: Fix bug #80037
 [2020-08-31 10:19 UTC]
-Status: Assigned +Status: Closed
 [2020-08-31 10:25 UTC] miloslav dot hula at gmail dot com
Thank you!
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 22:01:28 2024 UTC