What does the application gain by using Type Hint?

Asked

Viewed 1,212 times

8

In PHP 5 we know that the hinting type and now we can declare and force the type of parameter a function will receive. I believe it will help in the processing of the data more precisely, but what is the gain of using it, since it does not work also with primitive types?

All I can think about right now is code readability, but then we have tags of documentation that we can inform the type of the parameter and the return of that function.

Some discussions about hinting type:

Sopt 53476#53476

Sopt 48825#48825

Soen

2 answers

7


From the information the question shows already have the answer can not escape the obviousness.

In addition to the best documentation as already mentioned, and this cannot be minimized, it gains type security. This way the code requires the argument in the call to be of the specified type. This is already explained in the questions linked.

The fact of not allowing other types harms that the entire application has this security, but some security is better than none. And as probably in the future it will exist for all types (it already exists now), it is already halfway in the current application.

In larger systems it is much more robust to do this. It’s too bad for now you can’t make it wear everywhere, or it means you want this guy to be really dynamic. In small systems, i.e., scripts, It’s not important, but in large, it’s hard to keep everything running properly without offering guarantees. This medium is easier and more effective than doing treatment in the function body or writing tests to check whether the types were used correctly. You gain productivity and robustness, even if limited until you can use all types.

  • It is worth mentioning the declare(strict_types=1) that avoids the magic cast for the expected type?

  • This is new, right? It’s quoted :D

5

An addendum to the existing answer in PHP7 ceases to be called hinting type and is called type declaration 'cause this one now supports the guys int, bool, float and string, in addition to those already existing in php5, such as classes, interfaces, functions and arrays, I put a more detailed explanation in:

In practice the hinting type (php5) or type declaration php7 are optional and technically you can check the variables without using them, so for example:

PHP5

With induction of types:

<?php
function filterLetters($a) {
    if (is_array($a)) {
        return array_filter($a, 'ctype_alpha');
    }

    return false;
}

//Causa erro
var_dump(filterLetters(array('a', 'b', 'cdef', 0, 1, 3, 'a1')));

//Causa erro
var_dump(filterLetters('abc'));

With induction of types:

<?php
function filterLetters(array $a) {
    return array_filter($a, 'ctype_alpha');
}

//retorna array(a, b, cdef)
var_dump(filterLetters(array('a', 'b', 'cdef', 0, 1, 3, 'a1')));

//causa erro
var_dump(filterLetters('abc'));

PHP7

No type declaration:

function unixtimeToTimestamp($a) {
    if (is_int($a)) {
        return gmdate('Y-m-d H:i:s', $a);
    }

    return false;
}

//Retorna algo como 2001-09-11 10:10:30
var_dump(unixtimeToTimestamp(1000203030));

//Retorna false
var_dump(unixtimeToTimestamp(1123123123.5));

However see that it was necessary to create a if and use is_int, now in PHP7 you can do something like:

declare(strict_types=1);

function unixtimeToTimestamp(int $a) {
    return gmdate('Y-m-d H:i:s', $a);
}

var_dump(unixtimeToTimestamp(1000203030));

//Causa erro
var_dump(unixtimeToTimestamp(1123123123.5));

of course in this case we use declare(strict_types=1); to avoid some that would be considered implicit" for a "cast", such as float for int

Completion

Notice how much easier it got with the hinting type or with the type declaration? That’s the basic purpose of it(s).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.