array creation as reference, or always copied?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mortoray@ecircle-ag.com

    array creation as reference, or always copied?

    Throughout the PHP manual, and normal code, one finds this construct:
    $var = array( ... );

    However, as far as I can tell this is quite inefficient, since it
    always means two arrays are being created. The right-side array is
    created, and then copied to the left-side variable.

    Thus one would expect the normal syntax should be:
    $var =& array( ... );

    But this is not used in the manual very often.

    So, is there some kind of magic on the first array assignment such that
    it is done by reference (the first time)? Or are the vast majority of
    examples of array creation in the manual (and most PHP code)
    inefficient?

  • Janwillem Borleffs

    #2
    Re: array creation as reference, or always copied?

    mortoray@ecircl e-ag.com wrote:[color=blue]
    > However, as far as I can tell this is quite inefficient, since it
    > always means two arrays are being created. The right-side array is
    > created, and then copied to the left-side variable.
    >
    > Thus one would expect the normal syntax should be:
    > $var =& array( ... );
    >
    > But this is not used in the manual very often.
    >[/color]

    Using an array by reference would only be useful if you want to re-use it:

    // Create new arrays
    $array = array(1,2);
    $array2 = array(3,4);

    // Create a reference
    if (somecondition) {
    $array_ref =& $array;
    } else {
    $array_ref =& $array2;
    }

    // Do something with the array
    print $array_ref[0];


    JW



    Comment

    • Chung Leong

      #3
      Re: array creation as reference, or always copied?

      <mortoray@ecirc le-ag.com> wrote in message
      news:1108540356 .712929.201860@ o13g2000cwo.goo glegroups.com.. .[color=blue]
      > Throughout the PHP manual, and normal code, one finds this construct:
      > $var = array( ... );
      >
      > However, as far as I can tell this is quite inefficient, since it
      > always means two arrays are being created. The right-side array is
      > created, and then copied to the left-side variable.
      >
      > Thus one would expect the normal syntax should be:
      > $var =& array( ... );
      >
      > But this is not used in the manual very often.
      >
      > So, is there some kind of magic on the first array assignment such that
      > it is done by reference (the first time)? Or are the vast majority of
      > examples of array creation in the manual (and most PHP code)
      > inefficient?
      >[/color]

      array() is a declaration, not a function. That's why you can do things like

      function Dingo($baby = array())

      and

      class Dingo {
      var $baby = array();
      }


      Comment

      • Wayne

        #4
        Re: array creation as reference, or always copied?

        On 15 Feb 2005 23:52:36 -0800, mortoray@ecircl e-ag.com wrote:
        [color=blue]
        >So, is there some kind of magic on the first array assignment such that
        >it is done by reference (the first time)? Or are the vast majority of
        >examples of array creation in the manual (and most PHP code)
        >inefficient?[/color]

        As other people have pointed out Array() is not a function. But I
        thought I should also add that PHP uses copy-on-write for all
        operations meaning that there is no performance penalty for passing
        arrays (and strings, I believe) around. Only when you modify an
        array is a copy actually made. This is all done, of course, behind
        the scenes.

        Comment

        Working...