Bug #80705 [Opn->Csd]: If you unset a class property, the __get remembers the original type.
| From: | nikic@php.net | Date: | Wed, 03 Feb 2021 19:34:24 +0000 |
| Subject: | Bug #80705 [Opn->Csd]: If you unset a class property, the __get remembers the original type. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231935@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80705&edit=1
ID: 80705
Updated by: nikic@php.net
Reported by: vali dot dr at gmail dot com
Summary: If you unset a class property, the __get remembers
the original type.
-Status: Open
+Status: Closed
Type: Bug
Package: Scripting Engine problem
Operating System: Linux
PHP Version: 8.0.1
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This is not a bug. By design, it is impossible to bypass property types in *any* way, including via
__get.
This is also specified at https://wiki.php.net/rfc/typed_properties_v2#overloaded_properties.
Previous Comments:
------------------------------------------------------------------------
[2021-02-03 19:31:40] vali dot dr at gmail dot com
Description:
------------
If you unset a property on a class, the __get magic method gets called (good), but it's return
must match the original, now unset, property type. (bug?)
There should be a way to fully remove that property, either by reflection or unset (including the
return time)
Test script:
---------------
<?php declare(strict_types=1);
class Sample {
public int $foo = 123; # Start as in INT
public function __construct() {
unset($this->foo); # Remove the property
}
public function __get(string $name) { # Gets called, since
foo dies not exist.
echo "__GET\n"; # Validate that it's called
return "string"; # Cannot return string since foo was an int...
}
}
$test = new Sample();
echo $test->foo; # Throws error, since __get returns a string instead of an int
Expected result:
----------------
__GET
string
Actual result:
--------------
__GET
PHP Fatal error: Uncaught TypeError: Cannot assign string to property Sample::$foo of type int in
... .php:18
Stack trace:
#0 {main}
thrown in ...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80705&edit=1