Codementor Events

Advantages of Type Declarations and Return Type Declarations

Published Jun 23, 2017
Advantages of Type Declarations and Return Type Declarations

PHP is a power-house in server-side programming, and with the new release of PHP 7, PHP has proven to be among the best programming languages on the web.

There are many new useful features in PHP 7, but one of the most important features to take advantage of is the ability to use type declarations and return type declarations. The inability to use these features was one of the weaker points of earlier versions of PHP, but these new additions put PHP right alongside server-side languages such as C#. For those who are new to Type Declarations and Return Type Declarations, I am going to explain these features to you and why they are coveted by programmers.

In earlier releases of PHP there was no need to define a variable type. PHP automatically recognized the variable type based on how you used it within a function or program statement. This made the PHP language easy for beginners to learn, because they didn't have to worry about variable types. They just needed to have a name for their variable in order to execute a program. Although this may be useful for simple programs, there are times where being able to define a variable type would add to the functionality and overall readability of the program. Here is an example of traditional PHP code:

$name = "Keith"; 
$name = 10;

Since no type declaration is needed, the PHP compiler would understand that when $name is used in a statement that uses literal strings, the value should be assigned to the first $name variable, and when it is used in a statement that uses integer values the value should be assigned to the second $name variable. As you can see, too much of this could create very confusing code, and a project could get messy very quickly. In PHP 7, the above statement would be written as such:

string $name = "Keith";
int $name = 10;

Novice programmers, and those more familiar with traditional PHP, may feel that this only provides more bytes of code to be processed. Although that may be the case, being able to declare a variable as a specific type not only makes the code more readable for other programmers who may be working on the code, but it also serves as a valuable tool, especially when you need a function that requires a certain type of variable returned from it.

function enroll(Student $student, array $classes) {
    foreach ($classes as $class) {
        echo "Enrolling " . $student->name . " in " . $class;
    }
}

That brings me to Return Type Declaration, which is another great new feature of PHP 7. PHP programmers who have experience with other server scripting languages have been waiting for a feature like this to be added to PHP since its inception. This new functionality alone ensures that PHP 7 is going to be a powerful force and allow for more elegant and robust back-end web applications. I am going to explain why this new feature is so sought after by PHP lovers.

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

Every function has a specific job to do, even if it is as simple as displaying a message to the screen. The function is not the program itself, the function just provides the job for the main program. Once the function has completed its job, it "returns" its results to the main program. If there was an error executing the function, or the function couldn't successfully do its job, if programmed correctly the function will return an error exception to the main program so that the program will know that something went wrong.
Just know that something is always returned to the main program so that the main program can continue executing lines of code. Without a return value, the main program may not execute properly.
In earlier versions of PHP, there was no need to define a return type for a function. Again the compilers assumed the return type based off the content of the variables at the time of execution. This makes code easier to learn, but not easier to understand. Using Return Type Declarations allows you more control over what is actually returned from a function and leads towards program readability and smoother execution of the main program. Let's say for example you had a program that required a function that multiplied two numbers and returned the product back to the main program in order to be displayed. In traditional PHP it may look sort of like this:

function getTotal($num1, $num2) {
  $product = 0;
  $total = $num1 * $num2;
  return $total;
}

Now the above code may seem easy to read and understand. The GetProduct function takes in two variables(num1 and num2), once the variables have entered the function, they are multiplied together and the value is assigned to the product variable which is then returned to the main program. This is great if the values that are contained in num1 and num2 are both integer values, but what if one is an integer and one is a string? Then we have a problem. Declaring the return type prevents this problem because you could declare the return type to be an integer in order to get the exact results that you need. This makes it easier for you, your team, and the compiler.

These great new features of PHP 7 are essential updates that should be explored while building your new back-end programs. Hopefully you have gained from this information.

Discover and read more posts from German G.
get started
post commentsBe the first to share your opinion
Uri Frazier
2 years ago

Well written! Very easy to understand and presents great reasons for using Type Declarations. Thanks for your your efforts, German!

Show more replies