Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


PHP References explained



When should references be used?

When you're passing on large text, array or object structures to functions or passing data trough nested function calls. You should also perform benchmarking of your PHP code to find out places that need references, often using references will reduce execution time significantly.

When should references not be used?

When creating functions which usually is fed with constants values. PHP cannot create references to unassigned data, except for new object instances. For instance consider this example

function doIt( &$a )
{
  // Do something with $a
}

doIt( "some text" ); // Will not work

$txt = "some text";
doIt( $txt ); // Will work


The latter will always work but is cumbersome when you're using these functions often.

Links

References explained in the PHP manual
foreach structure
key function


<< Previous page | 1 | 2 | 3 | 4 | < 5 > | Printer-friendly page |

Comment List


Topic: Author:
Time:
Definition of large? Coco Loco 12.04.2003 13:17

On page 4, under "When Should References be Used", you say "When you're passing on large text, array or object structures to functions".

What do you consider "large"? It seems to be pretty subjective. Is an array with 100 elements considered "large"? 500? 1000? How about an array with 1 big element?

Thanks for the articles BTW, not many high-level PHP articles around. :)


Misleading Lance Lovette 04.03.2002 19:53

Some of the statements in this article are misleading if you are using PHP 4. You should read the article "PHP 4: Reference Counting and Aliasing" for more perspective - http://www.zend.com/zend/art/ref-count.php. According to that article the term "reference" is more appropriately described as "alias", there is no implicit deep copying on variable assignment and there isn't always a performance gain when passing references as function parameters.


   RE: Misleading Jan Borsodi 11.03.2002 16:08

> Some of the statements in this article are misleading if you
> are using PHP 4. You should read the article "PHP 4:
> Reference Counting and Aliasing" for more perspective -
> http://www.zend.com/zend/art/ref-count.php. According to
> that article the term "reference" is more
> appropriately described as "alias",

Using "alias" can be misleading as well, consider the last example on page 1.
By using "alias" one might believe that $b and $c is a different name for the variable $a and that when $a is unset so are $b and $c, which is not the case.

> there is no
> implicit deep copying on variable assignment and there isn't

Maybe my choice of words were bad but there's still a "copy" going on, whether this is smart and does internal referencing or just copies all the bits is not the point. The point is understanding what references do.

> always a performance gain when passing references as
> function parameters.

I didn't say there always is a performance gain, but that it might.
Allthough PHP 4 has reference counting I have successfully optimized my code by using references several places.
I've also seen the opposite where references would make the code slower.




Forgot your password?

Register a new user

Results

Polls