Re: [PHP-DEV] make casts more strict in PHP 7

[email protected] (Rowan Collins)
Newsgroups php.internals
Message-ID <[email protected]>
On 11 September 2014 04:02:43 GMT+01:00, Park Framework <[email protected]> wrote:
>> You know, “int”, “float”, “string” etc. aren’t currently reserved
>words.

Well, they are in the context of casts:

$a = (int)'42';
$a = (string)42;
etc

> We could add functions, or even make them reserved words and add
>internal functions (à la isset), so int(‘2’), float(‘2.0’),
>string(‘test’) etc.

How would this be different from what we already have? For the function version (useful as a callback), we have intval(), strval(), etc

>>
>> Or perhaps class-like constructors? $a = new int('2'); ? Would kinda
>make sense given some people want to add methods to primitive types.
>
>+1
>
>$int     instanceof Int
>$float  instanceof Float
>$num  instanceof Numeric
>$bool  instanceof Boolean
>$str     instanceof String
>$array instanceof Array
>
>This makes it possible not to use the is_*() functions.
>It is desirable to conduct instanceof fully consistent with behavior
>hinting casts, it would be logical and correct.
>Just wanted to be able to use these classes (or interfaces) to create
>their objects, these objects would pass inspection in hinting casts.

This could cause a lot of confusion. If these "constructors" are just a new syntax for type casting, there's no obvious reason to behave differently in terms of strictness, which is what we were discussing previously. Worse, they would look like objects, but behave as plain variables (no reference-like ability to modify the value in place).

Alternatively, these could be special "boxed types", which were real objects acting almost but not quite the same way as the equivalent basic type. Some languages (e.g. Java) have those, but they're frankly rather confusing, and I'm not sure what the benefit would be.

Regards,
-- 
Rowan Collins
[IMSoP]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.