June 2, 2018

Strict type usage in php is a good idea

Php has grown a lot the last couple of years and especially since php7 we've been given some feature which were quite overdue in my opinion. One of them being strict type usage

Strict type usage in php is a good idea

Ever since i started using php i heard a lot of "experienced" developers saying php is a bad, insecure, poorly designed language.
Maybe in some context they are right. There are some aspects missing in php which more mature languages like Java, C etc... do have.
But php has grown a lot the last couple of years and especially since php7 we've been given some features which were quite overdue in my opinion.
One of them being strict type usage.

Before i get into the topic i would like to note that even though php still allows for weakly typed code and type juggling which makes some code hard to read and understand, this is a developers choice. The developer still makes the choice how he or she writes their code. Everything can be written easily, fast and dirty. But that is not directly the error of the language. Bigger languages might prevent this all together but this doesn't mean it cannot be done correctly in php.

That being said, let's get into strict type usage. Since php7 we've been given the option to add this line to our php files.

declare(strict_types=1);

What does this provide us with? Well, very simply put, with this added to your file, php is no longer able to change your variable types into a different type if that enables a method to use that variable. An easy example:

Weakly type checking

<?php

$first = '1';
$second = 1;

function add(int $a, int $b): int
{
    return $a + $b;
}

print add($first, $second);

The first variable is a string, the second an integer.
We have defined a function which says the first and second parameter have to be integers. We didn't declare the strict type usage. So php changes the string '1' into an integer 1.
Personally i think a developer should prevent this situation even if php handles it for you. Because the result here will still be 2 (int).

Strictly reading this code the result doesn't make sense. We're telling it the method needs parameters both of type integer and were passing it a string and an integer. An error here would make more sense.

So that's why in my opinion the strict types declaration is a very good addition. If the code would look like this:

Strictly type checked

<?php
declare(strict_types=1);

$first = '1';
$second = 1;

function add(int $a, int $b): int
{
    return $a + $b;
}

print add($first, $second);

An error would be thrown because we're trying to use a string where an integer is expected. So this makes sense again. Because now the code is doing exactly what we're saying it should do and no other way.

Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given, called in /var/www/html/index.php on line 43 and defined in /var/www/html/index.php:38 Stack trace: #0 /var/www/html/index.php(43): add('1', 1) #1 {main} thrown in /var/www/html/index.php on line 38

If you're writing code keep in mind that chances are other people have to read and/or edit your code. So it should make sense and be readable.
Errors caused by type juggling hard often hard to find and detect.

A common misconception is that the strict type declaration affects your project or entire code base. But know that the strict type checks are only applied to the files you add the declaration.

So this is a great addition to php. It enables developers the option to force themselves to create clearer code and enforce logical written code. Php still allows for weakly typed code but now it fully depends on the developer how he or she wants to write their code.
But i would strongly encourage the usage of this feature.

PHP vs. other languages in "developers" eyes

Every language has it's advantages and it's disadvantages. If you're a php developer reading this you might have also gotten remarks about php being a crappy language and other remarks of that kind. Just remember that even though php allows for the code to be written crappy it's still up to you whether or not you write code in a bad way.

If you're a developer and you've ever handed out this sort of remarks, remember that you are probably not the best developer in the world (and even if you are). Don't bash on other peoples code simply for sake of wanting to prove some senseless point or showing off how great you are.
If you really see code that is poorly written, help them. Give them tips on how they can do it better next time. Send them pull requests or links to documentation. Discuss with them. You'll probably end up learning something yourself.

Conclusion

Strict type checking is great option. Use it! :)

Reference
PHP RFC: Scalar Type Declarations | For some examples