Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive method find if object $this error

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hello all, I was wondering why I am getting an error when calling the $this-> method in my class, here is what I mean:
Code:
class tree {
	function recurse($obj)
	{
		foreach($obj as $key=>$val){
			if(is_object($val)){
				$this -> recurse($val);
				
			} else {
				echo $key . ":" . $val;
			}
		}
	}
}
    $p = new stdClass;
	$c1 = new stdClass;
	$c11 = new stdClass;
	
	$c1->name = 'Michelle';
	$c11->name = 'Abby';
	
	$p->name = 'Dan';
	$p->child = $c1;
	$c1->child = $c11;
	
	tree::recurse($p);
When I run the code I get this:
Code:
name:Dan
Fatal error: Using $this when not in object context in C:\xampp\htdocs\mptt\AddObject.php on line 7

Why cant I call $this in my method?

Thanks,
timgerr


-How important does a person have to be before they are considered assassinated instead of just murdered?
So there you go! You're the retarded offspring of five monkeys having butt sex with a fish-squirrel! Congratulations!


 
Well, you're calling the method statically. That's what the double colon :):) means. If you want to use $this, you have to call it as an instance method, which means you have to instantiate an object of that class. So instead of your last line, do something like this:
Code:
$tree_instance = new tree();
$tree_instance->recurse($p);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top