Can I define the type of variables in the parameters of a method?

0

As in many programming languages, such as Java, C #, and many others, can you define in PHP the type of variable we want to initialize in a method's parameters?

The code below is an example of what I'm talking about, but in Java

//Nesse método, foi especificado que a variável "i" deverá ser do tipo int
public void executar(int i) {

}
    
asked by anonymous 16.01.2018 / 02:25

1 answer

1

You can define whether the type is array or object. In PHP 7 this functionality has been extended, and today it is possible to use this with int, float and the like. To use this functionality is very simple. Example:

public function teste(int $a, string $b)
{
}

You can also specify the return type (PHP7) by doing this:

public function sum(int $a, int $b) : int 
{
}
    
16.01.2018 / 02:54