On Tue, 24 Mar 2020 at 14:28, Reindl Harald <h.rei...@thelounge.net> wrote:

>
> Am 24.03.20 um 15:23 schrieb Rowan Tommins:
> > On the other hand, this is exactly the kind of thing where
> strict_types=1 makes things worse - you'll actually get *better* error
> output if you use strict_types=0 and pass the string to a function marked
> as requiring int
> no!
>
> with strict_types=0 the casting simp,y happens by the caller and you get
> no error at all
>


Only if the string is a valid integer; compare https://3v4l.org/uvPYZ with
https://3v4l.org/h1aT5

function foo(int $x) { var_dump($x); }

declare(strict_types=1);
$a = 'hello';
$a = (int)$a; // cast doesn't produce any errors
foo($a); // dumps int(0)

declare(strict_types=0);
$a = 'hello';
foo($a); // TypeError: Argument 1 passed to foo() must be of the type int,
string given


That's what I mean about "stricter casting" - (int)$a basically always
succeeds, so it would be useful to have a version that rejects things like
non-integer strings. That could be Yet Another Runtime Mode using
declare(), but it could just be a different syntax, so that you'd write
this:

// regardless of which strict_types mode you're in
$a = 'hello';
$a = (int!)$a; // TypeError: string value is not valid for strict cast to
type int
foo($a); // statement never reached


Regards,
-- 
Rowan Tommins
[IMSoP]

Reply via email to