NYCPHP Meetup

NYPHP.org

[nycphp-talk] I need a very elementary-school explanation of ' &'

Chris Bielanski Cbielanski at inta.org
Thu Feb 19 16:13:08 EST 2004


My quick and dirty answer is "don't worry that much about optimization for
space unless you a) know you must and b) have working code to optimize" :)

so if you read Passing By Reference and related information, it goes like
this:
Pass By Value is assumed unless you use & in the assignment such as "=&" or
"function &foo(){return &foo}"
Pass by Value is essentially copying all the apples from the apple cart,
leaving you with a cart full of apples, and the original cart full.

Pass by Reference (using &) makes a copy of the apple cart. That means that
if the original apple cart is unset, cleaned up, or changed, then your apple
cart *should* undergo the same rigor.

Pass By Reference is sometimes called "pass by handle" because the words
"Reference" and "Handle" are pretty synonymous to most people.
your snippets:

> $obj =& new Object(); // this is a new Object, looks ok!
> $result =& $obj->getResult(); // this provides you with the HANDLE to the
variable returned in getResult()

If &obj goes away, $result will probably react unfavorably, because $obj is
responsible for all of its members, and $result is actually an "alias" of
whatever getResult() kicks out.

> 
> as opposed to
> 
> $obj =& new Object();
> $result = $obj->getResult();

Simply, now anything can happen to $obj and $result will not change because
of it - you received the VALUE (not the HANDLE or REFERENCE) of the variable
kicked out by getResult()

HTH

~Chris

> -----Original Message-----
> From: Phil Powell [mailto:phillip.powell at adnet-sys.com]
> Sent: Thursday, February 19, 2004 3:55 PM
> To: NYPHP Talk
> Subject: [nycphp-talk] I need a very elementary-school 
> explanation of '&'
> 
> 
> $obj =& new Object();
> $result =& $obj->getResult();
> 
> as opposed to
> 
> $obj =& new Object();
> $result = $obj->getResult();
> 
> Could someone please explain this to me in a very, VERY simple 
> not-had-my-beer-yet way?  Readings at www.zend.com and 
www.php.net were 
fruitless, they might as well have been in Hungarian.  Sorry but this is 
a serious hindrance in my further PHP development because I always 
thought that by doing this you are saving memory:

$result =& $obj->getResult();

That means $result points the same value in memory as $obj->getResult(); 
thereby saving memory allocation and performance.  How off is that?

Thanx
Phil

-- 
Phil Powell
Web Developer
  ADNET Systems, Inc.
  11260 Roger Bacon Drive, Suite 403
  Reston, VA  20190-5203
Phone: (703) 709-7218 x107   Cell: (571) 437-4430   FAX: (703) 709-7219
EMail:  Phillip.Powell at adnet-sys.com      AOL IM: SOA Dude




_______________________________________________
talk mailing list
talk at lists.nyphp.org
http://lists.nyphp.org/mailman/listinfo/talk



More information about the talk mailing list