Typing the return in PHP 7. What are the advantages?

Asked

Viewed 3,299 times

14

I was giving a test in PHP 7 and I checked that the same now accepts to define which type of data will 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 should be of the float type

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’t give opinions, because I come from PHP 5 and I don’t know these practices.

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

How this will help the PHP developer?

4 answers

22


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

It is important to find out the error as early and near as possible from where it actually occurred. When it explodes away from the cause it’s much harder to find it.

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

Obviously if all the code is written this way can avoid the use of the function in places where another type is expected.

Of course, it also helps to better document intent. It even helps if someone does a maintenance and changes or creates an alternate execution path that generates a different type of return than expected. Before this could go unnoticed.

It is a shame that it is optional, so PHP is still a language that the programmer needs to worry about the return in most cases (this has improved somewhat in newer versions). In others he never really had to worry because the documentation already guaranteed that he would return only one type.

The gain is still not nearly what it could be if everything were typed, but it is a gain.

  • I liked the other places of code where it is more difficult to discover.

  • Yeah, blowing up somewhere else is one of the things that drives programmers crazy the most.

  • There was a colleague of mine who searched the entire system for a variable that he never found. He spent a week searching. Then he contacted the programmer and discovered that he had used the variable variable. $$pog = 'pog'

15

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

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

In dynamic typed languages, depending on what is returned, it is necessary to check if the return is within the expected, because it can be something completely unforeseen, depending on how the system is made.

How this will help the PHP developer?

It will help to avoid unwanted and unforeseen returns of functions. PHP ceases to be a language whose function returns "anything" to return something defined, which ensures a convenience to the programmer, who does not need to worry about checking the returned data structure.

  • 4

    If the programmer uses return typing, kkkkkkk. + 1

11

The advantage of typing the return method, means that the promised will be fulfilled or your money back in the form of 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 little is inference of types in the statements and methods/functions.

0

Typing goes beyond the guarantee of return type and good programming practices. I believe the main reason is the conscious use of memory resource. The PHP programmer does not understand the difference in the memory allocation of a variable of type String to an 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 more suitable in a given routine, because it influences and very much in the performance of the system.

  • 2

    What’s best to save memory in PHP? An array of ints, or a string? (considering the same information, only stored in the 2 ways)

Browser other questions tagged

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