Edit report at https://bugs.php.net/bug.php?id=79206&edit=1
ID: 79206
Comment by: greedy dot ivan at gmail dot com
Reported by: greedy dot ivan at gmail dot com
Summary: Access to an uninizialized property generates Error
instead of TypeError
Status: Feedback
Type: Documentation Problem
Package: *General Issues
PHP Version: 7.4.2
Block user comment: N
Private report: N
New Comment:
> Regarding the RFC itself, it is not the final documentation
Thank you for this clarification.
PHP docs contains almost nothing about typed property feature and how it works. All references in
Internet goes to that RFC. So, it is assumed that reading implemented PHP RFC is a good point to
know how feature works and many details about it.
Maybe PHP RFC page needs some disclaimer that implementation details may differ from what in on the
RFC page itself.
Previous Comments:
------------------------------------------------------------------------
[2020-02-01 22:32:00] requinix@php.net
> If this is a documentation error, how to fix it?
I don't see where in the documentation (php.net/manual) it says accessing an uninitialized
typed property results in a TypeError.
Regarding the RFC itself, it is not the final documentation. Minor details may change during
discussion and implementation and sometimes they don't always make it back into the originating
RFC.
> if this is an implementation error, it should be fixed.
I don't believe that TypeError is proper for this situation:
https://www.php.net/manual/en/class.typeerror.php> There are three scenarios where a TypeError may be thrown. The first is where the argument type
> being passed to> a function does not match its corresponding declared parameter type. The second is where a
> value being returned> from a function does not match the declared function return type. The third is where an invalid
> number of> arguments are passed to a built-in PHP function (strict mode only).
I don't see a more suitable error available
https://www.php.net/manual/en/reserved.exceptions.php
so a generic Error seems appropriate.
------------------------------------------------------------------------
[2020-02-01 22:22:58] greedy dot ivan at gmail dot com
If this is a documentation error, how to fix it?
if this is an implementation error, it should be fixed.
------------------------------------------------------------------------
[2020-02-01 22:01:04] requinix@php.net
Yes, that's right. What about it?
------------------------------------------------------------------------
[2020-02-01 12:55:52] greedy dot ivan at gmail dot com
Description:
------------
> Reads from uninitialized properties will generate a TypeError
-- https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties
Actually, it generates Error.
Test script:
---------------
<?php
class Foo {
public int $bar;
}
$foo = new Foo();
try {
$foo->bar;
} catch (\TypeError $te) {
var_dump('TypeError catched');
} catch (\Error $e) {
var_dump('Error catched');
} catch (\Throwable $t) {
var_dump('Throwable catched');
}
Expected result:
----------------
TypeError catched
Actual result:
--------------
Error catched
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79206&edit=1