Array Items By Value

../_images/array_by_value.png

In this code, an array is built with objects. The array is passed by value to the function. The function updates both the array and one of the elements. When the function is finished, the array in the calling context is still the same, but the object #2 has changed.

In this case, PHP applies copy on write, or COW: the array is passed by value, and duplicated only when it is updated. But the copy applies to the objects, which are always passed by reference!

Here, the elements in the array are references, and their update is applied to the original object, in the calling context.

This is a similar situation than with readonly properties, and array_fill(): the reference to the object is unchanged, but the object itself may be updated.

In general, objects may be considered as global values.

See Also

PHP Features