Req #79669 [Com]: Nullable variables
| From: | bugreports at gmail dot com | Date: | Wed, 03 Jun 2020 11:24:06 +0000 |
| Subject: | Req #79669 [Com]: Nullable variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227286@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79669&edit=1
ID: 79669
Comment by: bugreports at gmail dot com
Reported by: valentiny510 at gmail dot com
Summary: Nullable variables
Status: Feedback
Type: Feature/Change Request
Package: Variables related
Operating System: nix
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
> I want to be able to assign a variable without knowing if is set or not
$x ??= 'my default'; does exactly that
Previous Comments:
------------------------------------------------------------------------
[2020-06-03 11:18:02] valentiny510 at gmail dot com
Deeply in my heart I want the error removed, "Undefined variable: x"
is absolutelly useless and "too verbose" for my taste
I want to be able to assign a variable without knowing if is set or not
$a = $b; or $a = $c['d']['e'];
In the mayority of cases you need to check if is null anyways
if ($a)... or if (!is_null($a))...
But, since this is very unlikely to occur, at least, Yes, I want a shorter
version of the coalesce operator, Like I already said...
$a = $b ?? null; means "if is null, null, else null"
and I want...
$a = $b; meaning "if is null and I don't have any other option, then leave it null"
The parse could simply ask
"is set $b ?" then evaluate to null (as it already does), then ask again
"I have ?? after $b?" if no, return the null that Already Evaluated...
Is simple,
and maybe... maybe may cause (in some extraordinary exceptions)
some issues where you can have two options,
1. that $b can be set but have null assigned
2. is not set
and you can not decide, but I think that is isset( ) function for
So, if none of this is possible, at least I want to be extended the
"nullable things" to variables and maybe other objects like classes
$a = ?(new some_class); # nullable class/object (very unlikely but...)
in this case if some_class doesn't exists, can be converted automatically to a
"new null" where all functions return always null (maybe similar to stdClass)
and yes, I know I can implement that in the autoloader but maybe you don't want
that all classes be converted to null
and yes, you can also do $a = class_exists(...) ? new ... : new Null; but...
In this bug/request I want at least one of this two things
$a = ?$b; # nullable variable, or
$a =? $b; # nullable operator...
More feedback?
Thank you
------------------------------------------------------------------------
[2020-06-03 09:29:10] danack@php.net
I have no idea what you are saying. Please write clearly:
i) The code that you are currently writing that you don't like.
ii) a description of why that is a problem.
I get that you want something to be shorter, but you haven't said what.
------------------------------------------------------------------------
[2020-06-03 09:20:47] valentiny510 at gmail dot com
Description:
------------
The coalescing operator is one of the best life (or write) savers,
and still in some situations became a bit redundant, assuming $b is not set...
$a = $b ?? null; meaning "if is null, null, else null"
And we have to do this only to avoid the errors flooding the log...
(yes, I know we can disable it with error_reporting, but that will also disable other errors that we
may want to see)
Another good thing is the nullable type, but sadly it only apply to... types
So, my request is to extend the "nullable" to other... things,
like variables for instance, and I think will be a great life saver
Ex: $a = ?$b;
Seeing the above assignment, probably will be better to create a new operator,
=?
Meaning of course, assign the right side if isset, otherwise null
Thank you
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79669&edit=1