Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110361 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71163 invoked from network); 4 Jun 2020 11:18:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Jun 2020 11:18:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3119C1804FD for ; Thu, 4 Jun 2020 03:00:56 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f177.google.com (mail-il1-f177.google.com [209.85.166.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 4 Jun 2020 03:00:55 -0700 (PDT) Received: by mail-il1-f177.google.com with SMTP id g3so5429690ilq.10 for ; Thu, 04 Jun 2020 03:00:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IigOGzaIfJc2Sqp3likJ1SaR6U7ZTDtaflc3wNWxf+E=; b=bRxeMtUMetQcfiijfDJtQWDmbjOnHe6LX8EFm/EphyEmtol1XK/5vQyGpX72/56k2H tttficPCfNOBk+NeC5eSf2tp+ZjHNa+ajU8om0b2gETHPm3psoyfYrIaSA/Wvlt09zMG syY3pzC8l7Z+oSkZyAH4TYr3rHIT7keJU6iuOsnkoDAY1F+nVbNRhnyYcLypD49F0nFr 0nGGWasIamwC0xWlBNSiXlXlEQGtQF2W4NGevjpq5cV1HuXCLqYd7PLrqHoDugxBvtIW Lpd7NziRVuHy6nhg9f2VZm1OGebbLKJwUb5UMlVD5MajGeENDjCm47vL2ltm3H+1OPYP knYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IigOGzaIfJc2Sqp3likJ1SaR6U7ZTDtaflc3wNWxf+E=; b=klEVfkm9n8bMzL7hl9DF1EYJdr241hoj65AIx95SrivLTr0mkDlnBFtiCtO0cyfiz8 hSDNRlRJogxNemX5UyBhNtqk8yUbv2LjETRv2mItp/zZhE1/KdsBmNeO0+Ts2fDksbgY MKczz0poM1djRi9bPUt5Yd/E//w+ugUC75gmLI8b8qmrwOgReY5HQTcXr62l8Bv8/Le9 2tPER/vaO+SUpbrQDuQ8e07ze+T6uXF2n7o3QE79kke+Mle+ZVhjqqZ2YnkWFNHr0CIw TXm7eX/7gaaKSVkfE1yxg0QpESaENfV5CO9oFnOJ2KMR77LKeAPoOOisLUI8H4GL3F7f i5kw== X-Gm-Message-State: AOAM532xouLRsoN+gZUYBtPgqqufrd7dEz1pAZ2CgmJ1q2zwLj6wskW1 nol2m7lRppGQUVHsAVrfcVKc1gMHYYPOc2yYHFI= X-Google-Smtp-Source: ABdhPJz1Fv3e1/nXVaQQmS7T+ibUjTMh9e4vVWLg6+8OgWYL3jb1xbU4wpTJVXIuXETxK9308qiTZ1EOyjh7l+fe9Kw= X-Received: by 2002:a05:6e02:eb0:: with SMTP id u16mr3431966ilj.81.1591264851960; Thu, 04 Jun 2020 03:00:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 4 Jun 2020 03:00:40 -0700 Message-ID: To: Ilija Tovilo Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Nullsafe operator From: sarkedev@gmail.com (Peter Stalman) > Hi internals > > I'd like to introduce another RFC I've been working on: > https://wiki.php.net/rfc/nullsafe_operator > > It introduces the nullsafe operator ?-> that skips null values when > calling functions and fetching properties. In contrast to the last few > attempts this RFC includes full short circuiting. > > Let me know what you think. > > Ilija Hi Internals, Wouldn't it make more sense to make `isset` smarter? So that it can handle these situations just like it would with array access. That way you could use the null coalescing operator as well. I don't see many situations where you would be selective in which method in the chain you would want to make nullsafe and which ones you wouldn't. I think it would mostly be the entire chain, in which case using `??` would be much less verbose. In the few cases where you want to be specific in which are nullsafe and which ones aren't you can introduce one more variable. Using the first example in the RFC this would visully be far nicer and more readable about the intent: $country = $session->user->getAddress()->country ?? null; Besides, shouldn't the example really be: $user->getAddressCountry(); I don't recall ever having to nest three levels like that example. I know the example is contrived, but wouldn't you know if you have a logged in user or not? Then just have a simple getter on the user for what you need to check. Chances are those variables you're trying to save will be used elsewhere, or if not, refactor. Also, assuming we have control over the User class, you can either make address a property or use the magic getters (that many frameworks have, love or hate them). Even the new new accepted Constructor Property Promotion would be helpful in facilitating that. Basically we can easily get around this problem already, and some frameworks do it by default. If not, writing one getter where you have to check a few things is IMO far nicer than a long chain of possibly null objects. This is how I would solve the issue, assuming we're just in the controller we don't make anything specifically for the User or Address classes and no other uses for those specifically are needed. protected function getCountry() { $user = $this->session->user ?? null; if ($user === null) $this->redirectToLogin(); if ($user->getAddress() === null) return null; return $user->getAddress()->country ?? null; } Chaining that many objects together that may or may not be null just seems so wrong to me. My concern is that we would start seeing `?->` far too often and it would promote some lazy habits, because people would add it "just in case". Personally I think it should be more explicit when we are null-checking, and since the `->` object access usually doesn't have spaces around it, it could "muddy the water". It reminds me too much of the proposed "unary null coalescing operator", which also reminds me of the error control operator. Maybe make the syntax `->@` to be more clear? Sorry if I come off a bit harsh, but I don't see the need for this as other solutions to this problem are more favourable IMHO. As much as it can be annoying sometimes, the `Call to a member function foo() on null` is a very useful error. Thanks, Peter