title: getName() call on SimpleXMLElement returns fatal error "call to member function on a non-object"
body: <p>I am experiencing a very odd error. I am writing a function to validate XML based on element names, in this case passed in as an array. First, I normalize the input to a <code>SimpleXMLElement</code> object; in this case I am passing in a DOMDocument, and I tested it, it is converting correctly to <code>SimpleXMLElement</code>.</p>
  
  <p>What confuses me is that calling <code>getName()</code> will throw the following:</p>
  
  <pre><code>PHP Fatal error:  Call to a member function getName() on a non-object
  </code></pre>
  
  <p>But using <code>exit($xml-&gt;getName())</code> (or <code>echo</code>) will return the correct result - the root element name!</p>
  
  <p>Relevant code:</p>
  
  <pre><code>function validateXML($xml, $format='') {
  (get_class($xml) !== 'SimpleXMLElement') ?
      ((get_class($xml) === 'DOMDocument') ?
          $xml = simplexml_import_dom($xml)
          : $xml = simplexml_load_string($xml))
      : $xml = $xml;
  $rootName = ($xml-&gt;getName());
  if ($rootName != $format[0]) {
      exit($xml-&gt;getName())));
  }
  </code></pre>
  
  <p>Also, in case it matters, the server is running PHP 5.3</p>
  
  <p>I am very new to PHP so help is extremely appreciated!</p>
  
  <p>P.S. To be clear about what exactly is going on, the following code:</p>
  
  <pre><code>function validateXML($xml, $format='') {
      echo(get_class($xml);
      $xml = simplexml_import_dom($xml);
      echo(get_class($xml);
      echo($xml-&gt;getName());
  }
  </code></pre>
  
  <p>outputs to the server</p>
  
  <pre><code>DOMDocument
  SimpleXMLElement
  'ElementName' (the correct root name of my XML document)
  </code></pre>
  
  <p>... so it appears to be working, except that the fatal error still gets thrown.</p>