[PHP-BUG] Bug #78809 [NEW]: Uninitialized typed properties

[email protected] ("krmgns at gmail dot com") Wed, 13 Nov 2019 19:16:46 +0000
Newsgroups php.standards
Message-ID <[email protected]>
From:             krmgns at gmail dot com
Operating system: Ubuntu 18.04
PHP version:      7.4.0RC5
Package:          PHP Language Specification
Bug Type:         Bug
Bug description:Uninitialized typed properties

Description:
------------
While throwing when try to access to any uninitialized typed property
(eg: $o->x == null), the codes below are just causing notice.

BTW, error message is so weird. We were expecting something like this;

"Do not touch that f**king TYPED PROPERTY, it's a f**king TYPED PROPERTY
and you poor should know your place while coding with highness PHP!
Respect and behave yourself!"

AND, while some other main stream languages allowing to access them (to
uninitialized properties) simply saying null (or nil), why PHP is
forcing us to use empty(), isset() or ?? operator? Does PHP want a to be
cruel language or enjoying to usage of empty() and other likely-stuff
above, denying us to do checks with null ($o->x == null)? Also it is not
just a null check issue, but to access in any other way (eg: $o->x < 0,
!($o->x ^ 0) etc).


All aside, I am not angry or hating PHP, but simply was expecting more
easiness, productivity and better development experience while coding
with PHP all after 15 years my own PHP experience...

Dropping here some links hoping you to check them;
https://twitter.com/krmgns/status/1194293566899675136
https://twitter.com/nikita_ppv/status/1194301509036658691 (nikic'
comment)
https://gist.github.com/k-gun/43ea9ea0e53d690fe4e23005a98e5cfa

Some other languages behavior for uninitialized properties.
https://gist.github.com/k-gun/514e8ad8cd9b95eadf8910ab10f4e373

Test script:
---------------
$a = new class { var int $i; };
var_dump($a->i == null);
var_dump($a->i++);

$a = new class { var string $s; };
var_dump($a->s == null);
var_dump($a->s .= "");

Expected result:
----------------
Fatal error: Uncaught Error: Typed property class@anonymous::$i must not
be accessed before initialization in /var/www/a.php:14

Fatal error: Uncaught Error: Typed property class@anonymous::$s must not
be accessed before initialization in /var/www/a.php:14

Actual result:
--------------
PHP Notice:  Undefined property: class@anonymous::$i in /var/www/a.php
on line 14
NULL

PHP Notice:  Undefined property: class@anonymous::$s in /var/www/a.php
on line 14
string(0) ""

-- 
Edit bug report at https://bugs.php.net/bug.php?id=78809&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=78809&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=78809&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=78809&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=78809&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=78809&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=78809&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=78809&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=78809&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=78809&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=78809&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78809&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=78809&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=78809&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=78809&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=78809&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=78809&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=78809&r=mysqlcfg