Page 1 of 1

Fatal error: Call to a member function on a non-object...

Posted: Sun Aug 19, 2007 8:49 pm
by ubergrafik
I have a piece of php code that looks like this:

Code: Select all

for($j = 0; $j < $itemCount; $j++){
	$item = $doc->menu->section[$i]->item[$j]->attributes();
	$itemText = $item['text'];
	$itemHref = $item['href'];
	$itemTarget = $item['target'];
	echo '<a target='.$itemTarget.' href='.$itemHref.'>'.$itemText.'</a></br>';
};
In an earlier area, an XML loader object ($doc) is created. It pulls data from an XML file. The above loop is nested inside another one which spits out sectrions, this one spits out menu items. The data loads nicely, but then I get an error:
Fatal error: Call to a member function on a non-object...
I have been reading about this error and most stuff talks about scope for variables and the like. THe part I don't get is that all the data roks fine, THEN I get the error. I would have thought that I would see nothing. Can some one please tell me what I am missing here.
Thanks

Posted: Sun Aug 19, 2007 9:05 pm
by RhapX
Make sure you're including the file that you have the variable $doc defined in. Also, make sure you define it as a new class.

Code: Select all

$doc = new CLASSNAME();
Replace YOUR classname where I put CLASSNAME(); Let me know if this is what you were looking for.

Posted: Sun Aug 19, 2007 9:08 pm
by ubergrafik
Yes, I am instantiating the XML loader object as $doc, in an earlier area. It runs the code correctly until the llop finishes, then the error is echoed. That's the puzzling part, it all works until the last moment...

Posted: Sun Aug 19, 2007 9:11 pm
by RhapX
Post your full code.

Posted: Sun Aug 19, 2007 9:17 pm
by ubergrafik
Ok, here is the full code. Because I am running php4, I am using the XML classes found at:
http://www.ister.org/code/simplexml44/index.html

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<?php
		require_once('includes/class/IsterXmlSimpleXMLImpl.php');
		$implMenu = new IsterXmlSimpleXMLImpl;
		$doc = $implMenu->load_file('menu.xml');
	?>	
<head>

</head>
<body>
	<?php
		// BUILD THE MENU FROM XML FILE
		// initilise the variables
		$sectionCount = 0;
		$itemCount = 0;
		$item = null;
		foreach($doc->menu->children() as $child ){
			$sectionCount = $sectionCount + 1;
		}
		for($i = 0; $i < $sectionCount; $i++){
			// spit out text of section
			$section = $doc->menu->section[$i]->attributes();
			$sectionText = $section['text'];
			echo '<h2>'.$sectionText.'</h2>';
			//
			foreach($doc->menu->section[$i]->children() as $child ){
				$itemCount = $itemCount + 1;
			}
			echo '<p>';
			for($j = 0; $j < $itemCount; $j++){
				// spit out text of section
				$item = $doc->menu->section[$i]->item[$j]->attributes();
				$itemText = $item['text'];
				$itemHref = $item['href'];
				$itemTarget = $item['target'];
				echo '<a target='.$itemTarget.' href='.$itemHref.'>'.$itemText.'</a></br>';
			};
			echo '</p>';
		};
	?>
</body>

Posted: Mon Aug 20, 2007 12:37 am
by ubergrafik
Ahhh!! I forgot to reset my $itemCount variable at the end of the second for loop, immediately after the echo '</p>'; line. Works perfect now! Thanks all.