php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79206 More typed properties docs: uninitialized state, behavior with __get/set, etc.
Submitted: 2020-02-01 12:55 UTC Modified: 2021-08-23 10:28 UTC
From: greedy dot ivan at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.4.2 OS:
Private report: No CVE-ID: None
 [2020-02-01 12:55 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-01 22:01 UTC] requinix@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2020-02-01 22:01 UTC] requinix@php.net
Yes, that's right. What about it?
 [2020-02-01 22:01 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-02-01 22:22 UTC] 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:32 UTC] 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:55 UTC] greedy dot ivan at gmail dot com
> 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.
 [2020-02-01 23:22 UTC] requinix@php.net
-Summary: Access to an uninizialized property generates Error instead of TypeError +Summary: More typed properties docs: uninitialized state, behavior with __get/set, etc. -Status: Feedback +Status: Verified -Package: *General Issues +Package: Class/Object related
 [2020-02-01 23:22 UTC] requinix@php.net
> Maybe PHP RFC page needs some disclaimer that implementation details may differ from what in on the RFC page
> itself.
1. Everything in the RFC is supposed to be correct and the implementation should not differ. What likely happened here is that the mailing list discussion resulted in the error being changed to an Error and the author forgot to update the RFC. Typed properties was a complicated subject to talk about.
2. The RFC is not supposed to act as documentation. The manual proper should cover everything. If there is no documentation then that is a problem.

And I don't see sufficient documentation. What's there now is just an example of the syntax.
https://www.php.net/manual/en/language.oop5.properties.php
 [2021-08-22 17:08 UTC] deleugyn+php at gmail dot com
Looks like https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.typed-properties describes how typed properties work and there's an example of how Error is thrown for unitilialized properties.

Perhaps this can be closed?
 [2021-08-22 20:47 UTC] requinix@php.net
-Status: Verified +Status: Feedback
 [2021-08-22 20:47 UTC] requinix@php.net
I'm satisfied with the updated documentation. @ivan?
 [2021-08-23 09:25 UTC] greedy dot ivan at gmail dot com
agreed
 [2021-08-23 10:28 UTC] cmb@php.net
-Status: Feedback +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-08-23 10:28 UTC] cmb@php.net
Fine.  Closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 10:01:29 2024 UTC