Clarification required with uninitialized typed properties and null-coalesce operator
Nobody has claimed this yet.
- Dominant language
- XML
- Stars
- 596
- Forks
- 890
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 55
Description
Hello there,
With this PHP version:
php -v
PHP 8.1.5 (cli) (built: Apr 21 2022 09:51:30) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies
I can use typed properties:
php -r 'class X { public int $y; };$x = new X(); $x->y = 42; echo $x->y;'
42
When uninitialized, they throw a Fatal error, which is fine and clear in the documentation:
php -r 'class X { public int $y; };$x = new X(); echo $x->y;'
PHP Fatal error: Uncaught Error: Typed property X::$y must not be accessed before initialization in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
php -r 'class X { public int $y; };$x = new X(); echo ($x->y ?: 1337);'
PHP Fatal error: Uncaught Error: Typed property X::$y must not be accessed before initialization in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
php -r 'class X { public int $y; };$x = new X(); $x->y = $x->y === null ? 42 : 1337; echo $x->y;'
PHP Fatal error: Uncaught Error: Typed property X::$y must not be accessed before initialization in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
But when using null-coalesce, we don't have any error raised:
php -r 'class X { public int $y; };$x = new X(); echo ($x->y ?? 1337);'
1337
php -r 'class X { public int $y; };$x = new X(); $x->y ??= 42; echo $x->y;'
42
php -r 'class X { public int $y; };$x = new X(); $x->y = $x->y ?? 42; echo $x->y;'
42
I cannot find any line in the documentation stating that "Null-coalesce does not throw error on uninitialized typed property and this is on purpose" so may you confirm if that's the case? Because it could also be that NULL coalesce were forgotten in the implementation of typed properties.
Personally, the behavior or allowing ?? and ??= for uninitialized properties is perfect for my needs, but I would like to ensure this is a behavior that will stay in time (and not me relying on a bug! if so, let me know whether I should repost this in the php-src bug tracker instead)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the PHP documentation sections covering typed properties and the null-coalesce operators, using the PHP 8.1 CLI examples in this report to verify the behavior. Done means the documentation explicitly explains how ?? and ??= handle uninitialized typed properties and whether that behavior is intentional.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100