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

Asked

Viewed 34 times

0

Just as it is done in various programming languages, such as Java, C# and many others, you have to define in PHP the type of variable that we want to be initialized in the parameters of a method?

The code below is an example of what I’m talking about, only in Java

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

}

1 answer

1


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

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

It is also possible to specify the type of return (PHP7), for this just do :

public function sum(int $a, int $b) : int 
{
}
  • Very good to know, now I’m a fan of PHP 7

Browser other questions tagged

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