Search code examples
phpphpdocphpstorm

PHPStorm and magic methods


I am using PHPStorm and have written a class that utilises the SimpleXML class. Everything is fine, except when I traverse an XML string I will get "Undefined Property" warnings.

$xml = simplexml_load_string($string); //Returns SimpleXML Element

echo $xml->childElement; //PHPStorm reports "Undefined Property

I believe this is because the magic properties are not properly defined in PHPStorm. Is anyone aware of a nice little work around? It annoys me because I am pedantic about having nice clean code (and IDE) and having warnings come up on a class is just awful!


Solution

  • I think I've solved it. If I encapsulate the nodes inside curly braces as strings, PHPStorm will ignore these.

    echo $xml->{'Parent'}->{'ChildElement'};
    

    This has the advantage of being consistent if you encounter an XML tag with a hyphen, for instance. $xml->{'Parent-Node'}