What does the application gain using Type Hint?

7

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

Immediately I only think about the readability of code, but then we have the tags of documentation that we can inform the type of the parameter and the return of that function.

Some discussions about type hinting :

SoPT 53476 # 53476

SoPT 48825 # 48825

SOen

    
asked by anonymous 25.08.2015 / 16:10

2 answers

7

From the information the question already shows, the answer can not escape the truism.

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

The fact of not allowing other types harms that the whole application has this security, but some security is better than none. And as probably in the future there will be for all types, you are already halfway in the current application. I do not really know if you'll have to wait that long.

On larger systems, it is much more robust to do this. It's a pity that for now you can not force it to be used everywhere or to say that you want the type to be dynamic anyway. In small systems, that is, scripts , it is not important, but in large it is difficult to keep everything running properly without offering guarantees. This medium is easier and more effective than doing the in-body treatment of the function or writing tests to check if the types were used correctly. It gains productivity and robustness, although limited until you can use all types.

    
25.08.2015 / 16:24
4

An addition to the existing answer in PHP7 ceases to be called type hinting and is called type declaration since this now supports the types int , bool , float and string , in addition to the existing ones in php5, like classes, interfaces, functions and arrays, I've put a more detailed explanation on:

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

PHP5

With type induction:

<?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 type induction:

<?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, you have to create a if and use is_int , now in PHP7 you can do something like:

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

var_dump(unixtimeToTimestamp(1000203030));

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

Conclusion

Did you notice how easy it became with type hinting or type declaration ? This is his basic purpose.

    
28.02.2016 / 18:17