Class method returns nothing

Hi All,

Bit stumped at the moment. I am running a method of my class and am getting the value but for some reason I cannot return it as it always renders as false.

The method is recursive but I have put help in so I can see what steps are being taken.


# Fucntion to get the top parent of a given guid (Returns a pages guid)
	public function getPageTopLevelParentGUID($permalink, $doc)
	{
		echo "<li>Searching";
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($doc);
		$xpath = new DOMXPath($xmlDoc);
		$pageParent = $xpath->query("//site/pages/page[@permalink='$permalink']")->item(0);
		if($pageParent->getAttribute("parent") != "")
		{
			echo "<li>Look Up Parent Permalink";
			# i need the parents permalink to check against
			$parentPermalink = $this->getNodeValue("name", $pageParent->getAttribute("parent"), "permalink", $doc);
			$this->getPageTopLevelParentGUID($parentPermalink, $doc);
		} else {
			echo "<li>Done";
			return $pageParent->getAttribute("guid");
		}
	}

If I echo $pageParent->getAttribute(“guid”); I get the desired result but If I assign:


$test = $this->getPageTopLevelParentGUID("example", "example");
echo $test;

I get nothing. Am I losing scope in method as it is recursive?

The if part doesn’t return anything.

You should change


$this->getPageTopLevelParentGUID($parentPermalink, $doc);

to


return $this->getPageTopLevelParentGUID($parentPermalink, $doc);

Sweet, Many thanks! - Should of seen that Returnable object needs to return an object. Doh!

No worries, we all overlook something from time to time :slight_smile: