I have strict error reporting. I have to use isset and it make me to write long, repetitive chains of variables in PHP. I have sometimes to write code like this:
if (isset($my_object->an_array[$a_variable])):
$other_variable = $my_object->an_array[$a_variable];
else:
$other_variable = false;
endif;
or
if (isset($my_object->an_array[$a_variable])):
return $my_object->an_array[$a_variable];
endif;
Sometimes it is longer and more complicated. It isn’t readable and take too much time to type. I’d like to get rid of it.
The question
Is there a way to write $my_object->an_array[$a_variable] only once?
In the end I have found two solutions.
I. There is operator
@in PHP. It is very dangerous, tough.http://www.php.net/manual/en/language.operators.errorcontrol.php
However, it is acceptable in my situation.
null. I’m fine with testing for this or using implicit conversions.$php_errormsgin extreme situations.The code example:
The downside is I don’t receive information about lack of quotes in brackets.
II. It is possible to use operator
&.NULLtoo.$php_errormsgvariable doesn’t work for undefined variables.The code example:
The lack of quotes problem: