passing function by reference

$this->result=& $result;

Page 1 of 1

5 Replies - 657 Views - Last Post: 24 December 2009 - 04:33 AM Rate Topic: -----

#1 ethereal1m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 214
  • Joined: 30-June 09

passing function by reference

Post icon  Posted 21 December 2009 - 02:37 AM

Dear all,
I found the following snippet of iterator from the web but I got a difficulty to understand it. Suppose the snippet:
class MyDbResultIterator {
	var $result;
	function MyDbResultIterator (& $result) {
		$this->result=& $result;
	}
	function fetch() {
		return mysql_fetch_array($this->result);
	}
	function size() {
		return mysql_num_rows($this->result);
	}
	function reset() {
		return mysql_data_seek($this->result,0);
	}
}


Function MyDbResultIterator is passed by reference. But I don't understand this part:
 $this->result=& $result;


What does it do?

Best regards,
ethereal1m :)

This post has been edited by ethereal1m: 21 December 2009 - 09:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: passing function by reference

#2 Keiran  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 06-December 09

Re: passing function by reference

Posted 21 December 2009 - 03:04 AM

"Returning by reference is useful when you want to use a function to find to which variable a reference should be bound" - php.net

Put simply it binds the function to the variable instead of making a copy of it
Was This Post Helpful? 0
  • +
  • -

#3 ethereal1m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 214
  • Joined: 30-June 09

Re: passing function by reference

Posted 21 December 2009 - 09:10 PM

@above me,
you are not answering my question....
Was This Post Helpful? 0
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: passing function by reference

Posted 22 December 2009 - 06:37 AM

View Postethereal1m, on 21 Dec, 2009 - 10:10 PM, said:

you are not answering my question....

Actually, he did answer you question, though maybe not as clearly as you'd like.

That syntax assigns a reference, in this case making $this->result a reference to the passed-in $result parameter. You can think of a reference as an alias, another name for the same variable, so that when one changes, so does the other. You can read up on references here.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,475
  • Joined: 27-December 08

Re: passing function by reference

Posted 22 December 2009 - 06:56 PM

Passing by reference is big in programming. Basically, somewhere you have an object. Rather than creating a copy of the object each time you need to modify it or access it in some way (say, in a method), you can pass a reference to the object, which is basically a pointer. You can then update the pointer and the result will be effected in the object, plus you have access to the other object. In PHP it is helpful and more memory efficient, especially since you have to do your own garbage collection.
Was This Post Helpful? 1
  • +
  • -

#6 ethereal1m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 214
  • Joined: 30-June 09

Re: passing function by reference

Posted 24 December 2009 - 04:33 AM

thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1