Bug #78904 [NEW]: Side-effects for uninitialised typed property

From: Date: Tue, 03 Dec 2019 22:24:35 +0000
Subject: Bug #78904 [NEW]: Side-effects for uninitialised typed property
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224030@lists.php.net to get a copy of this message
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)

« previous php.bugs (#224030) next »