Req #79669 [Com]: Nullable variables

From: Date: Wed, 03 Jun 2020 11:50:03 +0000
Subject: Req #79669 [Com]: Nullable variables
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227289@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: > What version? I have seen the RFC, but I don't think is implemented yet why don't you just do your homework and at least read the feature page of each new release or at least try it out instead of thinking? > and still ??= is 1 character larger than =? that's ridiculous and null coalescing is always ?? so you don't have any point because it's more important to be consistent within a programming language instead save one char https://www.php.net/manual/de/migration74.new-features.php Null coalescing assignment operator ¶ <?php $array['key'] ??= computeDefault(); // is roughly equivalent to if (!isset($array['key'])) { $array['key'] = computeDefault(); } ?> Previous Comments: ------------------------------------------------------------------------ [2020-06-03 11:42:52] valentiny510 at gmail dot com btw, thank you and still ??= is 1 character larger than =? :P it should be only ?= just like the others += *= etc... ------------------------------------------------------------------------ [2020-06-03 11:38:07] valentiny510 at gmail dot com Not in 7.4.3 What version? I have seen the RFC, but I don't think is implemented yet ------------------------------------------------------------------------ [2020-06-03 11:24:06] bugreports at gmail dot com > I want to be able to assign a variable without knowing if is set or not $x ??= 'my default'; does exactly that ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ 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=79669 -- Edit this bug report at https://bugs.php.net/bug.php?id=79669&edit=1

« previous php.bugs (#227289) next »