php.net |  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
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-31 09:51 UTC] nikic@php.net
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] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2020-08-31 10:19 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dfaa4768d28061c1012dc0a0be465a672bd446d8
Log: Fix bug #80037
 [2020-08-31 10:19 UTC] nikic@php.net
-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: Mon Oct 14 05:01:28 2024 UTC