Re: [PHP-DEV] Type casting syntax

This is only part of a thread. view whole thread
July 12, 2021 07:03 (Nikita Popov)
On Sat, Jul 10, 2021 at 11:10 AM Max Semenik> wrote:

> 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? >
To answer your question without commenting on the wider discussion: No, it's not possible to use this syntax. C-style cast syntax is hopelessly ambiguous -- there's a reason why modern languages always pick a different syntax for type casts. To give a simple example, (MyClass) (CastArg) is already valid PHP syntax and performs a call to the function stored in constant MyClass, with the argument CastArg. Regards, Nikita