php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81064 __get() called accessing existing property which failed with TypeError before
Submitted: 2021-05-21 02:00 UTC Modified: 2021-05-23 08:05 UTC
From: yo at drmad dot org Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 8.0.6 OS: Manjaro
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
42 - 23 = ?
Subscribe to this entry?

 
 [2021-05-21 02:00 UTC] yo at drmad dot org
Description:
------------
Catching a TypeError while assigning a wrong type value to a typed property makes that property 'disappear', calling __get() magic method if accessed afterwards, and failing with a assigning error, even if there is no assigning.

Tested on PHP 7.4.18 and PHP 8.0.6

Test script:
---------------
<?php
class test {
    public int $a;

    public function __get($p) {
        echo "$p doesn't exist?\n";
    }
}

$t = new test;
try {
    $t->a = "Wrong type";
} catch (TypeError $e) {}

echo $t->a;

Expected result:
----------------
PHP Fatal error:  Uncaught Error: Typed property test::$a must not be accessed before initialization in /tmp/test.php:15

Actual result:
--------------
a doesn't exist?
Fatal error: Uncaught TypeError: Typed property test::$a must be int, null used
in /tmp/test.php:15

("Cannot assign null to property test::$a of type int" in PHP 8)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-23 04:46 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2021-05-23 04:46 UTC] krakjoe@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP --
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.

This has been fixed in 8.1
 [2021-05-23 08:05 UTC] nikic@php.net
This is also fixed in PHP 8.0, just not released yet.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 08 03:01:27 2024 UTC