When I try to access info that is not presented in xml like so: $someInfo = $element->blabla->cats[0]->src;
PHP shows notice like this: Notice: Trying to get property of non-object
How would I settle the matter?
views:
40answers:
3
+2
A:
Either $element, blabla, or cats[0] is not an object, and thus can't contain any elements.
Use isset():
if (isset($element->blabla->cats[0]->src))
echo $element->blabla->cats[0]->src;
one isset() should do, no need to check every part consecutively.
This should do the job even if cats exists but is not an array.
Pekka
2010-02-27 01:09:57
A:
you can use isset to verify if object property exists, like this:
if (isset ($element->blabla) && isset ($element->blabla->cats) && etc..)
if you just don't want to see the notice, use error_reporting(E_ALL & ~E_NOTICE)
mathroc
2010-02-27 01:10:33
A:
Or alternative (but wrong way) you can suppress log in your php script by:
error_reporting(E_ERROR);
Which will force php to report only fatal errors.
Anyway, use Pekka solution.
Enrico Carlesso
2010-02-27 01:14:22