Bug #78904 [NEW]: Side-effects for uninitialised typed property
From: public at grik dot net
Operating system:
PHP version: 7.4.0
Package: *General Issues
Bug Type: Bug
Bug description:Side-effects for uninitialised typed property
Description:
------------
Unitialized properties make unpredictabe side effects.
In fact, unitialized property is a Schrödinger property - it both
exists and does not exist at the same time.
All popular frameworks use magic getters in base classes.
Having __get() called for uninitialized properties makes initialization
in declaration to be obligatory, or face unpredictable side effects, as
it was in Pascal 30 years ago.
Scripting languages were inteneded to help avoid obligatory
initialization in declaration.
Test script:
---------------
<?php
abstract class Base{
public function __get($name){echo 'magic';}
}
class A extends Base{
public string $x;
public ?string $y;
public $z;
}
$A = new A;
var_dump(get_object_vars($A)); //array(0) {}
var_dump(property_exists($A,'x'));//true
$A->z; // __get is not executed
$A->y; // magic - __get() is executed
$A->x; //Fatal error
Expected result:
----------------
array(3) {
["x"]=>
NULL
["y"]=>
NULL
["z"]=>
NULL
}
bool(true)
Actual result:
--------------
array(1) {
["z"]=>
NULL
}
bool(true)
magicmagic
Fatal error: Uncaught TypeError: Typed property A::$x must be string,
null used
--
Edit bug report at https://bugs.php.net/bug.php?id=78904&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=78904&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78904&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78904&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78904&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78904&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78904&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78904&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78904&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78904&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78904&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78904&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=78904&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78904&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78904&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78904&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78904&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78904&r=mysqlcfg
Thread (14 messages)
- public at grik dot net
- requinix@php.netBug #78904 [Opn->Nab]: Side-effects for uninitialised typed property
- nikic@php.netBug #78904 [Nab]: Side-effects for uninitialised typed property
- public at grik dot netBug #78904 [Nab]: Side-effects for uninitialised typed property
- nikic@php.netBug #78904 [Nab]: Side-effects for uninitialised typed property
- public at grik dot netBug #78904 [Nab]: Side-effects for uninitialised typed property
- nikic@php.netBug #78904 [Nab->ReO]: Side-effects for uninitialised typed property
- nikic@php.netBug #78904 [PATCH]: Side-effects for uninitialised typed property
- public at grik dot netBug #78904 [Com]: Side-effects for uninitialised typed property
- nikic@php.netBug #78904 [ReO->Csd]: Uninitialized property triggers __get()
- andrzej dot heczko at gmail dot comBug #78904 [Com]: Uninitialized property triggers __get()
- nikic@php.netBug #78904 [Csd]: Uninitialized property triggers __get()
- andrzej dot heczko at gmail dot comBug #78904 [Com]: Uninitialized property triggers __get()
- public at grik dot netBug #78904 [Csd]: Uninitialized property triggers __get()