Return typing in PHP 7. What are the advantages?

11

I was giving a test in PHP 7 and checked that it now accepts to define which type of data is going to be returned.

Here are some tests:

Defining the instance to be returned

function test_object(): stdClass {

    return new ArrayObject;
}
  

Fatal error: Uncaught TypeError: Return value of test_object () must be an instance of stdClass, instance of ArrayObject returned

Defining that the return must be of type float

function test_type(): float
{
    return 1; // Nesse caso, retorna float(1)
}

function test_type(): int
{
    return 1;// Nesse caso retorna int(1)
}

function test_type(): string
{
    return 1 + 1; // Nesse caso retorna string(2)
}

function test_type(): string {

    return array(1, 2, 3); // Fatal error
}
  

Fatal error: Uncaught TypeError: Return value of test_type () must be of the type string, array returned

Some people may even have positive views on this, but I can not give opinions, as I come from PHP 5 and I do not know these practices.

What are the advantages of using the return type definition (return type)?

How will this help the PHP developer?

    
asked by anonymous 21.10.2015 / 17:33

4 answers

18

The advantage is precisely what you saw and demonstrated in the question. The compiler has the condition to check if the type of data used in the return corresponds to what was specified by the function / method, generating an error in the place where it was caused and not propagated to other places of the code where it is more difficult to discover (if the language would be fully compiled would be even better).

It is important to discover the error as early as possible as to where it actually occurred. When it explodes away from the cause it becomes much harder to find it.

Without this, the error will only be noticed when using the wrong data.

Obviously if all the code is written in this way, you can avoid using the function in places where another type is expected.

Of course it also helps to better document the intent. It even helps if someone else does a maintenance and change or create an alternate execution path that generates a different type of return than expected. Previously this could go undetected.

It's a shame that it's optional, so PHP is still a language the programmer needs to worry about in most cases. In others he never really had to worry, because the documentation already guaranteed that he would return only one type.

The gain is not even close to what it could be if everything was typed, but it is a gain.

    
21.10.2015 / 17:42
13

What are the advantages of using the return type definition (return typing)?

The same as any language with type discipline: security and cohesion and data.

In languages with dynamic typing, depending on what is returned, you need to check whether the return is within the expected, because it may be completely unexpected depending on how the system is done.

How will this help the PHP developer?

It will help prevent unwanted and unanticipated returns from functions. PHP ceases to be a language whose function returns "anything" to return something definite, which assures convenience to the programmer, who does not have to worry about checking the returned data structure.

    
21.10.2015 / 17:45
9

The advantage of typing the method return means that the fiance will be met or his money back in error or exception.

Currently in PHP5, what can happen is to pass a different type than expected and wait for the failure to happen. What avoids this a bit is inference of types in declarations and methods / functions.

    
21.10.2015 / 18:00
0

Typing goes beyond guarantee of return type and good programming practices. I believe that the main reason is the conscious use of memory resources. The PHP programmer does not understand the difference in the memory allocation of a variable of type String to one of integer type and for this reason he deems it unnecessary. Machines have limited resources so it is very important to know which type of variable is most appropriate in a given routine, as this influences the system performance a lot.

    
19.07.2018 / 22:39