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
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
Le 31 juil. 2018 à 21:07, Sara Golemon pollita@php.net a écrit :
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
And, please, also consider settype($x, '?int')
.
I have userland functions in my code for those type of casts.
—Claude
Thanks a lot!
-
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). -
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. -
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:
Le 31 juil. 2018 à 21:07, Sara Golemon pollita@php.net a écrit :
On Tue, Jul 31, 2018 at 2:23 PM, David Rodrigues <
david.proweb@gmail.com> wrote:
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
And, please, also consider
settype($x, '?int')
.I have userland functions in my code for those type of casts.
—Claude
--
David Rodrigues
Thanks a lot!
- 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.
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.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
Pretty nice idea!
On Tue, Jul 31, 2018 at 22:06 Gabriel Caruso carusogabriel34@gmail.com
wrote:
Thanks a lot!
- 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.
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.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
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:Thanks a lot!
- 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.
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.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
--
David Rodrigues
Can someone give me permission to publish the RFC?
My username is: david.proweb
I've granted you RFC karma.
--
Christoph M. Becker