Search code examples
phpobjectxpathmemberprivate-members

PHP: How do I populate a member of my object with a string?


I've got a problem with offsets because when there's no title, rather than having <cd title="No Title">, I have <cd>, in my xml file.

When there is no title attribute, I want to add the string "No Title" to the title member of my cd object.

Please look at the following code:

//SEEMS TO WORK
foreach ($Steps as $step){
  $cd->SetCD($cdkey->nodeValue);
  $titleQuery = './@title';
  $nullTitle = "No Title Listed";
  $areTitles  = $xpath->query($titleQuery, $step);
  //DOES NOT WORK
  if (!$areTitles)
  {
    $entry->SetTitles($title->$nullTitle)
  }
  //END DOES NOT WORK
  //WORKS
  else{
    $entry->SetTitles($title->$titleQuery) 
  }
}

How can I change the lines between //DOES NOT WORK and //END DOES NOT WORK, or the rest of the code, such that I get the desired result:

 [title:Entry:private] => Array
                (
                    [0] => no title
                    [1] => no title
                    [2] => Bounce
                    [3] => Bound
                    [4] => Bounty
                )

instead of:

 [title:Entry:private] => Array
                (
                    [0] => Bounce
                    [1] => Bound
                    [2] => Bounty
                )

?

Thank you.


Solution

  • DOMXPath::query() returns a DOMNodeList so your statement

    if (!$areTitles)
    

    will never evaluate to true.

    Instead, try

    if (0 == $areTitles->length)