Where should I check the type of a variable using PHP 7?

With version 7, PHP introduced scalar types, types with null values, and return types. Using these functions is immediately useful (self-executing code, fatal errors at runtime, the list goes on), but I'm not sure at what point in my code I should start using them. I believe the answer should be "as soon as possible."

Prior to PHP 7, a lot of my code looks like an example class. My controller initializes the class and passes POST variables to it. Then I will pass the type variable in the class to make sure that all the vars are correct.

class User {
     public function createUser($username, $password, $some_float)
     {
          // Check types, validate vars and create user
     }
}

With PHP 7 I can do it

class User {
     public function createUser(string $username, string $password, float $some_float)
     {
          // Validate vars and create user
     }
}

, User . .

+4
1

, , , stackoverlow. .

, , , .

, ( - . ..) , . , , Model-Controller-View. , .

:. , - , . , , , .

, . HTML5, javascript , , , "" , , . . , HTML5, javascript, . - -go. , , .

:

.

, .

: . " ". , . . , .

0

Source: https://habr.com/ru/post/1691698/


All Articles