Bug #79620 [NEW]: Typed nullable attribute throws Fatal error
| From: | jiri dot fajman at designeo dot cz | Date: | Fri, 22 May 2020 10:08:18 +0000 |
| Subject: | Bug #79620 [NEW]: Typed nullable attribute throws Fatal error | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-227125@lists.php.net to get a copy of this message | ||
From: jiri dot fajman at designeo dot cz
Operating system: Ubuntu 20.04
PHP version: 7.4.6
Package: *General Issues
Bug Type: Bug
Bug description:Typed nullable attribute throws Fatal error
Description:
------------
PHP 7.4.6
Following code throws "Typed property A::$x must not be accessed before
initialization".
Expected behavior:
Method getX() returns null.
I cannot imagine any single usecase for this behavior and I consider it
a serious bug leading back to PHP 7.0 hacks for nullable types in method
signature.
Note: Omitting getter method is not acceptable since accessing non
existing attribute does not throw error.
Test script:
---------------
<?php
class A {
private ?string $x;
/**
* @return string|null
*/
public function getX(): ?string
{
return $this->x;
}
/**
* @param string|null $x
*/
public function setX(?string $x): void
{
$this->x = $x;
}
}
$a = new A();
var_dump($a->getX());
Expected result:
----------------
Expected result is that var_dump prints null, not crash with fatal error
Actual result:
--------------
Fatal error:
Typed property A::$x must not be accessed before initialization
--
Edit bug report at https://bugs.php.net/bug.php?id=79620&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79620&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79620&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79620&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79620&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79620&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79620&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79620&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79620&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79620&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79620&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79620&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79620&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79620&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79620&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79620&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79620&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79620&r=mysqlcfg