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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

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

Post 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
RhapX
Forum Commoner
Posts: 30
Joined: Mon Dec 05, 2005 5:24 pm
Location: Seattle, Washington

Post 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.
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post 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...
RhapX
Forum Commoner
Posts: 30
Joined: Mon Dec 05, 2005 5:24 pm
Location: Seattle, Washington

Post by RhapX »

Post your full code.
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post 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>
ubergrafik
Forum Newbie
Posts: 9
Joined: Wed Aug 15, 2007 1:04 am

Post 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.
Post Reply