I'm using SimpleXML. If the user input to my function is invalid, my variable $x
is an empty SimpleXMLElement Object; otherwise, it has a populated property $x->Station
. I want to check to see if Station
exists.
private function parse_weather_xml() {
$x = $this->weather_xml;
if(!isset($x->Station)) {
return FALSE;
}
...
}
This does what I want, except it returns an error:
Warning: WeatherData::parse_weather_xml(): Node no longer exists in WeatherData->parse_weather_xml() (line 183 of vvdtn.inc).
Okay, so isset()
is out. Let's try this:
private function parse_weather_xml() {
$x = $this->weather_xml;
if(!property_exists($x, 'Station')) {
return FALSE;
}
...
}
This behaves almost identically:
Warning: property_exists(): Node no longer exists in WeatherData->parse_weather_xml() (line 183 of vvdtn.inc)
All right, fine, I'll turn it into an exception and disregard it. I've never done this before and I'm not sure I'm doing it right, but I'll give it a try:
function crazy_error($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
...
private function parse_weather_xml() {
set_error_handler('crazy_error');
$x = $this->weather_xml;
if(!property_exists($x, 'Station')) {
return FALSE;
}
restore_error_handler();
...
}
That returns its own HTML page:
Additional uncaught exception thrown while handling exception.
Original
ErrorException: property_exists(): Node no longer exists in crazy_error() (line 183 of vvdtn.inc).
Additional
ErrorException: stat(): stat failed for /sites/default/files/less/512d40532e2976.99442935 in crazy_error() (line 689 of /includes/stream_wrappers.inc).
So now I'm laughing hysterically and giving up. How can I check for the existence of a property of a SimpleXML object without getting this error in one form or another?
$x
(AKA$this->weather_xml
) itself is not set, not that it doesn't contain aStation
node. – Gonagle