Thomas Mlynarczyk wrote:
> On 25/06/15 15:48, Christoph M. Becker wrote:
>
>> It is the only way to let the user (opposed to library developers)
>> decide whether weak or strict type checking should be applied.
>
> Shouldn't the mere presence (or absence) of scalar type hints in a
> function declaration serve this purpose? If you absolutely want weak
> typing, you simply don't use type hints (nor strict libraries).
That would allow non-scalar values to be passed without an (immediate)
indication. A weak scalar type "hint" would still restrict to "fitting"
scalars.
> I consider PHP's built-in functions as being equivalent to functions
> without type hinting. And thus the behaviour should not change from what
> it currently is.
With `declare(strict_types=1)` built-in functions are supposed to be
strictly type "hinted", e.g.
<?php
declare(strict_types=1);
sin("1");
will fail:
PHP Fatal error: Uncaught TypeException: sin() expects parameter 1
to be float, string given in ...
> According to the manual, the signature is
>
> string dechex( int $number )
>
> In your example, $num would be a float, larger than the allowed range
> for int.
Indeed, if we're assuming a 64bit build. On a 128bit build, $num would
be int. There are no 128bit builds yet, but basically, the same applies
to 32bit vs. 64bit builds.
> So, taking the manual literally, I would expect this to cause
> an error. (The actual output is "0", by the way.) The current behaviour
> would be better "hinted" with
>
> string dechex( numeric $number )
>
> which would describe the allowed values more accurately. (Assuming
> numeric = bool | int | float | string containing a number.) I do not see
> any "weird edge case" here. Currently, the manual is inaccurate by
> specifying "int" whereas the function also accepts bools, floats and
> numeric strings. The latter fact could even be regarded as an
> undocumented feature, since the documentation clearly says "int".
> Anyway, I wouldn't call this function with anything else but an integer
> argument.
PHP is a loosely typed language, and wherever a value of an unexpected
type is given it is automatically juggled to the expected type, if
feasible. Most PHP programmers are likely to be accustomed to this
behavior, and make use of it, at least from time to time.
However, the following fails:
<?php
declare(strict_types=1);
class Foo {
function __toString() {
return 'foo';
}
}
function bar(string $string) {
echo $string;
}
bar(new Foo);
Most likely the way to workaround this to cast `new Foo` to string, i.e.
`(string) new Foo`. Fine. However, I don't see why the function should
be type-"hinted" in the first place, because a cast will accept almost
arbitrary values, so the advantage to be able to detect programming
errors is practically gone.
Anyhow, you may consider taking one of your programs, adding
declare(strict_types=1) at the beginning of every file, and running it
under PHP 7.0.0alpha2 (which has been released today, by the way).
There may be some surprises. :)
--
Christoph M. Becker