Bug #80037 [Com]: Typed property must not be accessed before initialization when __get() declared
Edit report at https://bugs.php.net/bug.php?id=80037&edit=1
ID: 80037
Comment by: miloslav dot hula at gmail dot com
Reported by: miloslav dot hula at gmail dot com
Summary: Typed property must not be accessed before
initialization when __get() declared
Status: Closed
Type: Bug
Package: *General Issues
PHP Version: 7.4.9
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Thank you!
Previous Comments:
------------------------------------------------------------------------
[2020-08-31 10:19:42] 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 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)