Jump to content


Photo

[SOLVED] Desparation: References do not work as expected


  • Please log in to reply
3 replies to this topic

#1 yassen

yassen
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 August 2006 - 03:01 PM

Hello guys, first post here.

I am really desparate about how references (do not) work in PHP.

Here's a code snippet:

	var $data;
	var $_children;
	var $_uniqueNames;

	function &add(&$child) {
		$name = $child->data->name;
		if (array_key_exists($name, $this->_uniqueNames)) {
			return $this->_uniqueNames[$name];
		}
		$this->_children[] =& $child;
		$this->_uniqueNames[$name] =& $child; // we register the name
		$true = TRUE;
		return $true;
	}

This is part of a simple Node class prepared to build trees. A Node remembers the names of its children and refuses to add a new child with a name already registered. Everything seems to work except the return value in case there is an attempt to add a child with an already registered name -- my client code always gets NULL whatever I do!
I would appreciate any idea of why is that and how can I fix it, thanks in advance!
Yassen


#2 yassen

yassen
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 August 2006 - 04:17 PM

My worst fears are justified:

Not only newbies fail to understand how references work in PHP, nobody really knows  :-\


#3 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 14 August 2006 - 04:34 PM

You would have 2 use the function twice~~
and if you are trying to print the value please dont forget like noobs that you need to use echo if you use return, it just does not echo it.
I am a PHP Guru, ask me questions if you want to

#4 yassen

yassen
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 August 2006 - 07:28 PM

Thanks, RockingGroudon!
I finally got it -- it was my mistake, erroneous code in a wrapper function that substituted the return value for something else. Thanks again!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users