'Strict Mode' in PHP7 Is Not Strict Enough?

Recently I heard that in PHP7 we can pass a string as integer and get result in strict mode. Let’s try it together. Pay attention, behavior we check is already described in the documentation. It’s all being ‘not a bug, just a feature’. So, let’s go. As you know, now we can define strict mode using next instruction:

declare(strict_types=1);

So, try to write simple piece of code like this:

<?php
declare(strict_types=1);

function typesCheck(int $number) {
	echo $number;
	return $number;
}

typesCheck(3);

And now we will see ‘3’ on our screen. It’s absolutely predictable behavior. Now we will try to pass a wrong argument:

<?php
declare(strict_types=1);

function typesCheck(int $number) {
	echo $number;
	return $number;
}

typesCheck('qwerty');

And in response we will see an error like this:

Fatal error: Uncaught TypeError: Argument 1 passed to typesCheck() must be of the type integer, string given

It’s a logical behavior too, right? Also, in PHP7 we get possibility to catch type errors and I’ll explain it later. Let’s pass that value can be transformed from string to number:

<?php
declare(strict_types=1);

function typesCheck(int $number) {
	echo $number;
	return $number;
}

typesCheck('2');

And we also will get error. So, any unexpectedness here. Let’s check it without strict mode?

<?php
declare(strict_types=0);

function typesCheck(int $number) {
	echo $number;
	return $number;
}

typesCheck('2');

And in this case you will get, of course ‘2’;

Okay, it’s clear with passing arguments, let’s play a little game with return types. Here I a bit modified previous example :

<?php
declare(strict_types=1);

function typesCheck(int $number) : int {
	$hintedNumber = $number + 2;
	return $hintedNumber;
}

echo typesCheck(2);

You will see ‘4’ on the screen. Right. Now we will try to do any type hint here:

<?php
declare(strict_types=1);

function typesCheck(int $number) : int {
	$hintedNumber = $number + '2'; //'2' is string
	return $hintedNumber;
}

echo typesCheck(2);

And…. You will receive 4! In the strict mode. I checked a documentation and really, a type hinting isn’t disabled in strict mode. Сompletely illogical in strict mode as for me. Let’s modify this example a little bit more:

<?php
declare(strict_types=1);

function typesCheck(int $number) : int {
	$hintedNumber = $number + '2string'; //'2string' is string
	return $hintedNumber;
}

echo typesCheck(2);

4! Okay, I’m scared.

Attempts to add a string to number will return just a number (2 + ‘qwerqwer’) will return 2. Just default type hinting behavior for PHP5.

Just for calm, let’s try…

<?php
declare(strict_types=1);

function typesCheck(int $number) : int {
	return '2';
}

echo typesCheck(2);

It throws error, really, this is a progress definitely.

So as you can understand, ‘strict mode’ relates only to functions arguments and return types but don’t provides us any related to strong typing in any case. Yes, it will help you to avoid a lot of problems but, I think, strict mode should be refined in the near future. And now I believe in that.