Bug #78744 [NEW]: Unexpected __set & __get behavior
| From: | dirk dot gerigk at tui dot com | Date: | Wed, 23 Oct 2019 16:30:34 +0000 |
| Subject: | Bug #78744 [NEW]: Unexpected __set & __get behavior | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-223420@lists.php.net to get a copy of this message | ||
From: dirk dot gerigk at tui dot com
Operating system: Linux
PHP version: 7.4.0RC4
Package: Class/Object related
Bug Type: Bug
Bug description:Unexpected __set & __get behavior
Description:
------------
Issue is that __get and __set does some strange things
when commenting in and out the $this->array = []; line.
Test the code and you will see.
This all started with "Typed Properties" and its is very strange.
So, is this normal behavior? If yes, why is that so?
Must Important Question:
Why are the magic methods connected to the property definition and the
error clearly says "X::$bar must be string, null used"? Because at this
point the __get returns NULL.
On a previous BUG Report (Bug #78226) someone sayed: Do always ?string
$foo = null;
But that can be the solution to this, or?
Test script:
---------------
class X
{
protected string $bar;
protected array $array = [];
public function __construct(string $bar)
{
$this->bar = $bar; $this->array = []; print $this->bar.PHP_EOL;
}
public function __set($name, $value)
{
print "set $name = $value".PHP_EOL; $this->array[$name] =
$value;
}
public function __get($name)
{
print 'get '.$name.PHP_EOL; return $this->array[$name] ?? null;
}
}
$obj = new X('FOO');
Expected result:
----------------
set bar = FOO
get bar
FOO
Actual result:
--------------
set bar = FOO
get bar
Fatal error: Uncaught TypeError: Typed property X::$bar must be string,
null used
--
Edit bug report at https://bugs.php.net/bug.php?id=78744&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=78744&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78744&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78744&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78744&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78744&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78744&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78744&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78744&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78744&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78744&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78744&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=78744&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78744&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78744&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78744&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78744&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78744&r=mysqlcfg