Bug #80037 [Asn->Csd]: Typed property must not be accessed before initialization when __get() declared

From: Date: Mon, 31 Aug 2020 10:19:42 +0000
Subject: Bug #80037 [Asn->Csd]: Typed property must not be accessed before initialization when __get() declared
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228816@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80037&edit=1

 ID:                 80037
 Updated by:         nikic@php.net
 Reported by:        miloslav dot hula at gmail dot com
 Summary:            Typed property must not be accessed before
                     initialization when __get() declared
-Status:             Assigned
+Status:             Closed
 Type:               Bug
 Package:            *General Issues
 PHP Version:        7.4.9
 Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2020-08-31 09:51:54] 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 08:43:18] 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.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80037&edit=1


Thread (4 messages)

« previous php.bugs (#228816) next »