# Nullable cast (?int)

- Thread: https://staging.externals.io/message/102997
- Original archive: https://news-web.php.net/php.internals/102997
- Started by David Rodrigues on 2018-07-31T18:23:02+00:00
- Messages: 8
- RFC: Nullable Casting (Inactive) — https://staging.externals.io/rfc/nullable-casting.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://staging.externals.io/search.md?q={query} · RFCs at https://staging.externals.io/rfc.md · JSON API at https://staging.externals.io/api · documentation at https://staging.externals.io/llms.txt

---

## #102997 — David Rodrigues — 2018-07-31T18:23:02+00:00

URL: https://staging.externals.io/message/102997#102997

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

---

## #102998 — Sara Golemon — 2018-07-31T19:07:30+00:00

URL: https://staging.externals.io/message/102997#102998 · In reply to: #102997

> 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 kept as null.
>
I like this proposal.  Short, simple, and consistent with other parts
of PHP syntax.  Write up an RFC for it?

-Sara

---

## #102999 — Claude Pache — 2018-07-31T19:50:00+00:00

URL: https://staging.externals.io/message/102997#102999 · In reply to: #102998

> Le 31 juil. 2018 à 21:07, Sara Golemon <pollita@php.net> a écrit :
> 
> > […]
> I like this proposal.  Short, simple, and consistent with other parts
> of PHP syntax.  Write up an RFC for it?
> 
> -Sara
> 

And, please, also consider `settype($x, '?int')`. 

I have userland functions in my code for those type of casts. 

—Claude

---

## #103001 — David Rodrigues — 2018-07-31T22:58:09+00:00

URL: https://staging.externals.io/message/102997#103001 · In reply to: #102999

Thanks a lot!

1. I never did a RFC and have no karma to do, so if someone wants to help
me with that, I appreciate (contact me directly, then we could publish RPC
link as reply here).

2. Too, I could not implementate it, and I don't know if it is a
requirement to write a RFC or if it could be done in another moment.

3. What PHP version it should be focused? 7.4, right?

Em ter, 31 de jul de 2018 às 16:50, Claude Pache <claude.pache@gmail.com>
escreveu:

>
>
> > […]
> david.proweb@gmail.com> wrote:
> > […]
> that
> > […]
> to
> > […]
>
> And, please, also consider `settype($x, '?int')`.
>
> I have userland functions in my code for those type of casts.
>
> —Claude
>
>

-- 
David Rodrigues

---

## #103003 — Gabriel Caruso — 2018-08-01T01:05:09+00:00

URL: https://staging.externals.io/message/102997#103003 · In reply to: #103001

>
> Thanks a lot!
>
> 1. I never did a RFC and have no karma to do, so if someone wants to help
> me with that, I appreciate (contact me directly, then we could publish RPC
> link as reply here).
>


Someone can give you acess to https://wiki.php.net/rfc to you email, so
you'll be able to write the RFC, no karma need for that.


>
> 2. Too, I could not implementate it, and I don't know if it is a
> requirement to write a RFC or if it could be done in another moment.
>
> 3. What PHP version it should be focused? 7.4, right?
>

Yes, all new features now should target PHP 7.4. Just write the RFC and
open the PR at https://github.com/php/php-src and let the process rollout.

Regards,
-- 
Gabriel Caruso

---

## #103004 — Marcos Passos — 2018-08-01T02:00:38+00:00

URL: https://staging.externals.io/message/102997#103004 · In reply to: #103003

Pretty nice idea!

On Tue, Jul 31, 2018 at 22:06 Gabriel Caruso <carusogabriel34@gmail.com>
wrote:

> > […]
> RPC
> > […]
>
>
> Someone can give you acess to https://wiki.php.net/rfc to you email, so
> you'll be able to write the RFC, no karma need for that.
>
>
> > […]
>
> Yes, all new features now should target PHP 7.4. Just write the RFC and
> open the PR at https://github.com/php/php-src and let the process rollout.
>
> Regards,
> --
> Gabriel Caruso

---

## #103020 — David Rodrigues — 2018-08-02T01:02:36+00:00

URL: https://staging.externals.io/message/102997#103020 · In reply to: #103004

Can someone give me permission to publish the RFC?
My username is: david.proweb

Thanks!

Em ter, 31 de jul de 2018 às 23:00, Marcos Passos <
marcospassos.com@gmail.com> escreveu:

> Pretty nice idea!
>
> On Tue, Jul 31, 2018 at 22:06 Gabriel Caruso <carusogabriel34@gmail.com>
> wrote:
>
> > […]
>

-- 
David Rodrigues

---

## #103023 — Christoph M. Becker — 2018-08-02T10:08:48+00:00

URL: https://staging.externals.io/message/102997#103023 · In reply to: #103020

> Can someone give me permission to publish the RFC?
> My username is: david.proweb

I've granted you RFC karma.

-- 
Christoph M. Becker
