I really like the casting syntax, but I think it is difficult to be
approved, at first, because there is already a "clearer" method for doing
something like that. Which for me, particularly, does not invalidate an
alternative.

My suggestion is to create a magic method like __cast(AllowedTypes $in):
self. Eg. __cast(string|int $in): BigNumber. Where AllowedTypes are the
classes allowed to be casted from (or "mixed").

So:

$bn = (BigNumber) '123';
$date = (Carbon) $date;
$timestamp = (Carbon) (int) $date;

I also wanted it to be possible to use a nullable cast, but the idea didn't
go forward (even though there were no alternatives). :(


Atenciosamente,
David Rodrigues


Em sáb., 10 de jul. de 2021 às 06:10, Max Semenik <maxsem.w...@gmail.com>
escreveu:

> I've been thinking about extending PHP's cast syntax to user-defined types,
> e.g. not only (int)$foo but also (MyClass)$foo. Currently, a T_STRING in
> parentheses is always treated as a constant - would it be acceptable to
> hijack this syntax when used in unary operation context, i.e. "(" T_STRING
> ")" expr? If not, any other alternatives?
>
> --
> Best regards,
> Max Semenik
>

Reply via email to