Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102997 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46270 invoked from network); 31 Jul 2018 18:23:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Jul 2018 18:23:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.176 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.223.176 mail-io0-f176.google.com Received: from [209.85.223.176] ([209.85.223.176:33026] helo=mail-io0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6D/C6-14835-519A06B5 for ; Tue, 31 Jul 2018 14:23:17 -0400 Received: by mail-io0-f176.google.com with SMTP id z20-v6so13876767iol.0 for ; Tue, 31 Jul 2018 11:23:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=VxK3Ka4yBUgvBnP+H6fmkMm2X1FSjS1BzesM4ziYuNM=; b=m1KKzVZefmY5d9fmoHdygSRehMVCqMiHb61Vhw5vHQZUDBpsfwKxiH75jIOlPeXVBb HM3+GiWfB2QWhvzDWIF9ZbBGLkrwZz0K9+IqSeH2IEvY2lWp3v2sKr87NnrLYnZ1dvX0 aHKzZDdlNHRw0SkYLl4FWYK0/Ery1ENB639V2Iio4C9XXDrKdAAhKkW5Y1QmDlVjYsSz Fhi1deHGJStRmCkyVy2NCnOEfX7lKJLpcvq0GzQEfhECjPFho+877GJDOS9AALLXCODD woYFWgN9osH1H/jJxQ1NFINmsUVF3T9iHkMhw/fyEPbatM0Y8GMy6HRe9HiYJa65xn5i brHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=VxK3Ka4yBUgvBnP+H6fmkMm2X1FSjS1BzesM4ziYuNM=; b=PtlBU49ZdthfMIvgixjRbXGSri1esKirgxENhfEW04VeB6wnG+3R/I20j5fdV0C384 92zLKbVrMdcB7iwobCttigiAgbtm0a9VtQ6Wkq7BkOQoK5pVBiy2fPIAdJAutrBskqpY 9Ej/9pLlOUfHnJ4v/RpyJFJBndzxhMbEOoLernPz6fWoAI38gdtNqYifUyS3Pago/7il cjNIjjmCLjhX24xxk8XKtB3Ro+bNQmQYJiGiHNkF+1dh3A96p7bFqZ2w+2ta8yTKHiow QeE3mX5qpu6/AxSNzoFgZ78dqHrLY3YIoStkyYwB6FEsk34u1sBEhXqcf9ZGzLATX4fs AfSw== X-Gm-Message-State: AOUpUlGvVmA4rFop8RK8gAkPKnISIu4xYhUbbBgON0AI+5RX45Mdja3t K3kOWR2tvStsb6ymq968TCrs+6fpMTI3nfDOXdvohw== X-Google-Smtp-Source: AAOMgpcPbIm19WHFi1yJAnFnhc1YS51uVA0sf3kqxK8KeZRihDYItDrRJbLWuBQ7Dt9I8abzXjR37uSAJNsb3k9GII4= X-Received: by 2002:a6b:1b53:: with SMTP id b80-v6mr742418iob.48.1533061393669; Tue, 31 Jul 2018 11:23:13 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 31 Jul 2018 15:23:02 -0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000c9e6a805724fab78" Subject: Nullable cast (?int) From: david.proweb@gmail.com (David Rodrigues) --000000000000c9e6a805724fab78 Content-Type: text/plain; charset="UTF-8" Currently we have support to (int) cast (and similar). But I do think that we too need a possibility to do a nullable cast. In terms, it will cast to (int) only if value is not null, else it should be keeped as null. $number = 123; (int) $number => 123; (?int) $number => 123; $string = '123'; (int) $string => 123; (?int) $string => 123; $null = null; (int) $null => 0 (?int) $null => null The last example is the biggest problem, because it transforms (int) null to 0, (bool) null to false, (string) null to '', (array) null to []. It could be an incorrect behavior when your parameter requires an ?int, where an int will have a different behavior than null. Example: function increase(?int $number): ?int { if ($number === null) { return null; } return $number + 1; } increase($_POST['number']); // Could causes increase() first argument should be ?int. increase((int) $_POST['number']); // Will works, but will be always int, and if $_POST['number'] is null, then it will fails because will be converted to 0 erroneous. increase((?int) $_POST['number']); // Will works if is an int, string or null, respecting the expected behavior. -- David Rodrigues --000000000000c9e6a805724fab78--