title: PHP Fatal Error on specific PHP installation - Call to undefined method DOMText::getElementsByTagName()
body: <p>I'm having a problem with code working on one PHP install and not working on the other. Maybe one install is more forgiving when it comes to the error.</p>
  
  <p>When I upload to production I receive the following error:</p>
  
  <pre><code> PHP Fatal error:  Call to undefined method DOMText::getElementsByTagName() in ...
  </code></pre>
  
  <p>The line causing the error is:</p>
  
  <pre><code>$tds = $tr-&gt;getElementsByTagName('td');
  </code></pre>
  
  <p>I have a feeling the issue is related to calling the <code>getElementsByTagName</code> method from within <code>DOMText::</code> instead of <code>DOMDocument::</code> (<a href="http://php.net/manual/en/domdocument.getelementsbytagname.php" rel="nofollow">the docs</a> seem to make this obvious) but out of my lack of understanding of what I've done wrong, I am not sure how to address the issue.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;?php
  
  // The HTML
  $table_html = '&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;AAA&lt;/td&gt;
              &lt;td&gt;BBB&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;aaa&lt;/td&gt;
              &lt;td&gt;bbb&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;';
  
  
  // Create DOM Document
  $document = new DOMDocument();
  $document-&gt;preserveWhiteSpace = false;
  $document-&gt;formatOutput = true;
  @$document-&gt;loadHTML($table_html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOEMPTYTAG);
  
  // Change TD's to TH's in THEAD's
  $theads = $document-&gt;getElementsByTagName('thead')-&gt;item(0);
  if ($theads) {
  
      foreach($theads-&gt;childNodes AS $tr) {
  
          $tds = $tr-&gt;getElementsByTagName('td'); // &lt;---- This is where the error occurs
          if ($tds-&gt;length &gt; 0) {
  
              $i = $tds-&gt;length - 1;
              while($i &gt; -1) {
                  $td = $tds-&gt;item($i); // td
                  $text = $td-&gt;nodeValue; // text node
                  $th = $document-&gt;createElement('th', $text); // th element with td node value
                  $td-&gt;parentNode-&gt;replaceChild($th, $td); // replace
                  $i--;
              }
  
          }
  
      }
  }
  
  // Output
  echo $document-&gt;saveHTML(); 
  </code></pre>