-2

I received this old script from a client which was still running on an old server with PHP 4.4. We placed it on a new server with PHP 5.6 and it doesn't work anymore how it was supposed to work.

The script saves a form to a XML file and retrieves the info from the XML into the form again. Problem is that it doesn't retrieve it anymore. We get this error:

PHP Fatal error: Call to a member function getAttribute() on null

Here is the part of the code where it goes wrong.

        if(!$xml_page->parseXML($strXMLToCContents)){
            $show_errors .= "Parsing failed.<br>";
        }

        $nodeRoot =& $xml_page->documentElement;

        $nodeToCDef =& $nodeRoot->firstChild;

        $strToCButtonLabel = $nodeToCDef->getAttribute("name");

        $countToCRows = $nodeToCDef->childCount;

        for($n = 0; $n < $countToCRows; $n++){
            $thisLinkNode =& $nodeToCDef->childNodes[$n];
            $arrLinks[] = array($thisLinkNode->getAttribute("name"), $thisLinkNode->getAttribute("page"));
        }
    }
}

if(count($arrLinks) == 0){
    for($n = 0; $n < $numFormFieldsToShow; $n++){
        $arrLinks[] = array('', '');
    }
}

Any ideas what needs to be changed to get it to work again. Thanks!

Phil
  • 157,677
  • 23
  • 242
  • 245
  • For starters, you can remove all those reference assignments – Phil Dec 27 '17 at 01:06
  • Which line is throwing the error? `$nodeToCDef->getAttribute("name")` or `$thisLinkNode->getAttribute("name")`? – Phil Dec 27 '17 at 01:07
  • Also, there's nothing stopping this from attempting to access the DOM if the parsing fails. – Phil Dec 27 '17 at 01:10
  • $nodeToCDef->getAttribute("name") – Dreamstar Dec 27 '17 at 01:11
  • May be a duplicate; see https://stackoverflow.com/questions/27914755/call-to-a-member-function-getattribute-on-a-non-object and https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983 – slevy1 Dec 27 '17 at 01:36

1 Answers1

1

Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions. As of PHP 7.0 it is syntactically invalid. (Technically, the difference is that, in PHP 5, object variables, much like resources, are a mere pointer to the actual object data, so these object references are not "references" in the same sense used before (aliases)..

http://php.net/manual/en/language.references.whatdo.php

Jonny
  • 1,319
  • 1
  • 14
  • 26
  • Does an `E_STRICT` level error cause the assignment to be `null`? Also, where is OP using `new`? – Phil Dec 27 '17 at 01:23