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



Arrays and references

You can also let array elements be references.

$a = array();
$b = "test";
$a[0] =& $b;
$a[1] = "ok";
$a["ok"] =& $b;


Now let's get back to the standard assignment operator, consider

$d = $a;


This creates a new copy of the data and refers to it in $d. This is known as deep copy and is what you've probably been using.
References are extremely important when you're working with complex object and array hierarchies.

All of PHP's iterating structures and functions does not create references but actually copies each element, this is OK when dealing with small data sizes but has a significant performance hit with larger data.

Arrays have a different behavior when using the assignment operator.

$b = "data";
$arr = array();
$arr["a"] =& $b;
$arr_copy = $arr;


This actually performs a copy of all the elements of the array but only a shallow copy, so the reference is only copied to the new element not the data.

Using the function var_dump you can easily spot elements which are references.
This means that array elements will be deep copied if they have real contents but will have a shallow copy when a reference is encountered.


<< Previous page | 1 | < 2 > | 3 | 4 | 5 | Next page >> | 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