Req #78480 [Opn->Wfx]: The typed properties miss an important "function"
Edit report at https://bugs.php.net/bug.php?id=78480&edit=1
ID: 78480
Updated by: krakjoe@php.net
Reported by: manchokapitancho at gmail dot com
Summary: The typed properties miss an important "function"
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: 7.4.0beta4
Block user comment: N
Private report: N
New Comment:
After discussion as a result of https://github.com/php/php-src/pull/7029
It has transpired that we collectively do not want to go in this direction at this time, and
it's highly unlikely this will change, in my opinion.
Closing as won't fix.
Anyone is free to pursue an RFC, which would be required - but ill advised given the overwhelming
negative feedback.
Previous Comments:
------------------------------------------------------------------------
[2021-02-26 09:58:05] heiko at jerichen dot de
Working with graphql provides an other use case.
When you have a ?float field with null, it means delete this value. When not provided with graphql
(would resolve to uninitialized) than ignore it.
------------------------------------------------------------------------
[2020-12-07 04:34:57] marrtins at dqdp dot net
I completely agree such is_initialized() would be useful for typed properties. Another use case
would be for passing data to database.
Distinction would be useful:
If property hasn't been initialized - ignore it.
If property has been set to null - set database field to null.
------------------------------------------------------------------------
[2020-12-04 10:50:17] a dot haan at iwink dot nl
Ah. With https://bugs.php.net/bug.php?id=78904
that behavior of __get changed. There's now a difference between uninitialized and
unset properties.
That makes this request more apparent.
We used to mix types for micro cache properties; start as false, ending up with
null, or an object instance. By strict checking for false retrieval is
only done once.
Since mixing types is a bad practice, and not an option with typed properties anyway, it would be
ideal if one could detect whether a property was actually uninitialized.
Sure, we can. Two ways are mentioned here. But both are verbose and require representing the
property as a string.
------------------------------------------------------------------------
[2020-12-04 10:15:52] a dot haan at iwink dot nl
[2019-09-08 18:10 UTC] jhdxr@php.net writes:
You can use __get to check if it's initialized.
That's not completely true - in PHP 7.4.11.
In the first example on https://wiki.php.net/rfc/typed_properties_v2#overloaded_properties
if you do not include unset($this->typed);, then that example still throws:
Fatal error: Uncaught Error: Typed property Test::$typed must not be accessed before initialization
in test.php on line 24
That line is the first var_dump($test->typed);.
Apparently this little edge case was not covered when implementing the RFC.
Regarding this mentioned issue there is another option to test for initialized state:
array_key_exists('prop', get_object_vars($obj));
Not very neat either.
------------------------------------------------------------------------
[2019-09-09 18:10:12] manchokapitancho at gmail dot com
I know that I can use __get but:
1. this is a workaround (which is also not simpler than a ReflectionProperty call).
2. this is only possible if the developer has control over the class. A counterexample would be a
typed property in a class exposed by a third party library.
Once again: the target is "if (!is_initialized($obj->prop)) { ... }" similar to
"if (!isset($obj->prop)) { ... }". Isset would not generate a notice if the property is
not defined and similarly is_initialized should not trigger a TypeError if $obj->prop has not
been yet initialized.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=78480
--
Edit this bug report at https://bugs.php.net/bug.php?id=78480&edit=1
Thread (10 messages)