Variable Type Check

I have methods whose parameters can only handle certain types of variables. I have a few ideas on how to test these types, and I need your help to choose the best way.
I could:

  • Just check back falseif one of the variable types is wrong, preventing the user from knowing what is going on. In addition, if the output of a function is usually not checked by the user - for example. ob_start()- they won’t know that it’s even wrong,
  • Throw away a custom InvalidArgumentExceptionsaying: "Type of parameter X is invalid." Thus, I have to check every parameter, making the code unreadable. Also, the exception really needs to be caught, and I don't like these try...catchin my code.
  • Challenge error_log(). But still I have to check every parameter.

Which option would you choose? What for? Otherwise, do you have an idea?


UPDATE
When I talk about types, I mean the following: http://php.net/manual/en/language.types.php

+3
source share
2 answers

The only way to type check in php is to use the built-in function. You can find the list here: http://www.php.net/manual/en/ref.var.php This is a real pain in the ass, but you have no choice.

For the verification itself, I will check all types of parameters at the beginning of the function and give an error if not. Then you can always add print_r debugging to detect the culprit.

0
source

, , .

:

function(){
    if (!is_array($arg))
       trigger_error();
}

,

:

function yay(Class1 $arg1, Class2 $arg2){
    //That it!
}
0

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


All Articles