I don't have enough experience with PHP and programming at all, so I want to know, what the best decision in my situation is. Here is my code sample:
public static function isAscii($string, $type, $length = -1)
{
if (!is_string($string))
throw new InternalException(ExceptionMessage::invalidFunctionParameterType(__CLASS__, __METHOD__));
if ($length !== -1 && !Str::isValidLength($string, $length))
return FALSE;
$ascii_array = Constants::asciiRegex();
if (!preg_match($ascii_array[$type], $string))
return FALSE;
return TRUE;
}
I want to ensure that key $type
exists for array $ascii_array
. What is the best way to do that?
I can offer two decisions:
- The simplest. Just check key existence manually with
array_key_exists()
function. If key doesn't exist - throw an exception, also manually. But I think that is not a good idea and checking this condition is redundant, because PHP compiler will generate warning if I don’t write anything (Notice: Undefined index:) and I want to use this somehow. I found there are some predefined exceptions likeOutOfBoundsException
andOutOfRangeException
, it would be easy just to use them like in Java, but it seems that PHP doesn’t throw these exceptions, only warnings and errors. - Use
set_error_handler()
function to 'convert' errors and warnings to exceptions. But there is another problem - how can I handle all possible errors? I tried to find the complete list of PHP errors and warnings, but it doesn't exist. So I have no idea how to solve this problem, please help me. Thanks in advance, hope you will understand my question even with my poor English level.
P.S. this is an array with regular expressions for different types of ascii strings.