{"id":6065,"date":"2014-04-12T02:58:55","date_gmt":"2014-04-12T02:58:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/check-for-propertys-existence-without-getting-node-no-longer-exists-warning-collection-of-common-programming-errors-2\/"},"modified":"2014-04-12T02:58:55","modified_gmt":"2014-04-12T02:58:55","slug":"check-for-propertys-existence-without-getting-node-no-longer-exists-warning-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/check-for-propertys-existence-without-getting-node-no-longer-exists-warning-collection-of-common-programming-errors-2\/","title":{"rendered":"Check for property&#39;s existence without getting &ldquo;Node no longer exists&rdquo; warning-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m using SimpleXML. If the user input to my function is invalid, my variable <code>$x<\/code> is an empty SimpleXMLElement Object; otherwise, it has a populated property <code>$x-&gt;Station<\/code>. I want to check to see if <code>Station<\/code> exists.<\/p>\n<pre><code>private function parse_weather_xml() {\n    $x = $this-&gt;weather_xml; \n    if(!isset($x-&gt;Station)) {\n        return FALSE;\n    }\n\n    ...\n}\n<\/code><\/pre>\n<p>This does what I want, except it returns an error:<\/p>\n<blockquote>\n<p>Warning: WeatherData::parse_weather_xml(): Node no longer exists in WeatherData-&gt;parse_weather_xml() (line 183 of vvdtn.inc).<\/p>\n<\/blockquote>\n<p>Okay, so <code>isset()<\/code> is out. Let&#8217;s try this:<\/p>\n<pre><code>private function parse_weather_xml() {\n    $x = $this-&gt;weather_xml; \n    if(!property_exists($x, 'Station')) {\n        return FALSE;\n    }\n\n    ...\n}\n<\/code><\/pre>\n<p>This behaves almost identically:<\/p>\n<blockquote>\n<p>Warning: property_exists(): Node no longer exists in WeatherData-&gt;parse_weather_xml() (line 183 of vvdtn.inc)<\/p>\n<\/blockquote>\n<p>All right, fine, I&#8217;ll turn it into an exception and disregard it. I&#8217;ve never done this before and I&#8217;m not sure I&#8217;m doing it right, but I&#8217;ll give it a try:<\/p>\n<pre><code>function crazy_error($errno, $errstr, $errfile, $errline) {\n    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);\n}\n\n...\n\n    private function parse_weather_xml() {\n        set_error_handler('crazy_error');\n        $x = $this-&gt;weather_xml; \n        if(!property_exists($x, 'Station')) {\n            return FALSE;\n        }\n        restore_error_handler();\n\n        ...\n    }\n<\/code><\/pre>\n<p>That returns its own HTML page:<\/p>\n<blockquote>\n<p>ErrorException: property_exists(): Node no longer exists in crazy_error() (line 183 of vvdtn.inc).<\/p>\n<h2>Additional<\/h2>\n<p>ErrorException: stat(): stat failed for \/sites\/default\/files\/less\/512d40532e2976.99442935 in crazy_error() (line 689 of \/includes\/stream_wrappers.inc).<\/p>\n<\/blockquote>\n<p>So now I&#8217;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?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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-&gt;Station. I want to check to see if Station exists. private function parse_weather_xml() { $x = $this-&gt;weather_xml; if(!isset($x-&gt;Station)) { return FALSE; } &#8230; } This does what I want, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6065","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6065"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6065\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}