Arrays and Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thehuby

    Arrays and Pointers

    I have come across an issue using arrays ond objects; I am using an
    array to keep track of 'n' number of object(The code snippet below is
    from my initial testing)

    I first declare the object ($nr = new news_resource() ;), then call a
    function of the object ($nr->params_init( ); ). This is the put into
    the first element of the db, I then call the function again and put the
    object in the next element array.

    However when I print it all out at the end, all the elements of the
    array store the object with the same values...

    if I redeclare the object ($nr = new news_resource() ; ) again, before
    calling the funciton then it gets added in fine.

    I'm sure its a typical behaviour and is to do with pointers - but I
    don't understand why reassiging the object ($obj = new my_class() )
    before callings its funciton should break the link to the original
    object (and maintain its state).

    In my mind it should either always put a copy in the array or else
    always be a pointer.

    Can anyone explain why PHP works this way?

    Code snippet below:

    $nr = new news_resource() ;

    $nr->params_init( 0, "www.num1.c om", "Link to num1", "This links to
    number 1", 0, 0 );
    $resources[] = $nr;

    $nr = new news_resource() ; //why does this mean that the code works
    and the array keeps a copy of the object instead of a pointer?
    $nr->params_init( 0, "www.num2.c om", "Link to num2", "This links to
    number 2", 0, 0 );
    $resources[] = $nr;

    $nr = new news_resource() ;
    $nr->params_init( 0, "www.num3.c om", "Link to num3", "This links to
    number 3", 0, 0 );
    $resources[] = $nr;



    Regards,
    Rick Huby


  • Oli Filth

    #2
    Re: Arrays and Pointers

    thehuby said the following on 29/09/2005 15:39:[color=blue]
    > I have come across an issue using arrays ond objects; I am using an
    > array to keep track of 'n' number of object(The code snippet below is
    > from my initial testing)
    >
    > I first declare the object ($nr = new news_resource() ;), then call a
    > function of the object ($nr->params_init( ); ). This is the put into
    > the first element of the db, I then call the function again and put the
    > object in the next element array.
    >
    > However when I print it all out at the end, all the elements of the
    > array store the object with the same values...[/color]

    Yes, because $nr is a reference to the news_resource object. Every time
    you add it to the array (with $resources[] = $nr), you're inserting
    *another* reference to the same object.
    i.e.:

    $a = new object();
    $b = $a;

    $b and $a refer to the *same* object. Anything you to do $b will happen
    to $a, because they're just aliases of each other.
    [color=blue]
    > if I redeclare the object ($nr = new news_resource() ; ) again, before
    > calling the funciton then it gets added in fine.[/color]

    In this case, you're redefining $nr as a reference to a new object, so
    when you do $resources[] = $nr now, you're storing a reference to a new,
    separate object.


    --
    Oli

    Comment

    • Justin Koivisto

      #3
      Re: Arrays and Pointers

      thehuby wrote:
      [color=blue]
      > I have come across an issue using arrays ond objects; I am using an
      > array to keep track of 'n' number of object(The code snippet below is
      > from my initial testing)
      >
      > I first declare the object ($nr = new news_resource() ;), then call a
      > function of the object ($nr->params_init( ); ). This is the put into
      > the first element of the db, I then call the function again and put the
      > object in the next element array.
      >
      > However when I print it all out at the end, all the elements of the
      > array store the object with the same values...
      >
      > if I redeclare the object ($nr = new news_resource() ; ) again, before
      > calling the funciton then it gets added in fine.
      >
      > I'm sure its a typical behaviour and is to do with pointers - but I
      > don't understand why reassiging the object ($obj = new my_class() )
      > before callings its funciton should break the link to the original
      > object (and maintain its state).
      >
      > In my mind it should either always put a copy in the array or else
      > always be a pointer.
      >
      > Can anyone explain why PHP works this way?
      >
      > Code snippet below:
      >
      > $nr = new news_resource() ;
      >
      > $nr->params_init( 0, "www.num1.c om", "Link to num1", "This links to
      > number 1", 0, 0 );
      > $resources[] = $nr;
      >
      > $nr = new news_resource() ; //why does this mean that the code works
      > and the array keeps a copy of the object instead of a pointer?
      > $nr->params_init( 0, "www.num2.c om", "Link to num2", "This links to
      > number 2", 0, 0 );
      > $resources[] = $nr;
      >
      > $nr = new news_resource() ;
      > $nr->params_init( 0, "www.num3.c om", "Link to num3", "This links to
      > number 3", 0, 0 );
      > $resources[] = $nr;[/color]

      Odd... Here's what happens for me:
      <?php
      class myTest{
      var $_val=NULL;
      var $_mark=NULL;
      function init($x){
      $this->_val=$x;
      }
      }

      $testar=array() ;
      for($i=0;$i<5;$ i++){
      $tmp=new myTest();
      $tmp->init($i);
      $testar[]=$tmp;
      }

      var_dump($testa r);
      ?>

      Output:
      array(5) {
      [0]=>
      object(myTest)# 1 (2) {
      ["_val"]=>
      int(0)
      ["_mark"]=>
      NULL
      }
      [1]=>
      object(myTest)# 2 (2) {
      ["_val"]=>
      int(1)
      ["_mark"]=>
      NULL
      }
      [2]=>
      object(myTest)# 3 (2) {
      ["_val"]=>
      int(2)
      ["_mark"]=>
      NULL
      }
      [3]=>
      object(myTest)# 4 (2) {
      ["_val"]=>
      int(3)
      ["_mark"]=>
      NULL
      }
      [4]=>
      object(myTest)# 5 (2) {
      ["_val"]=>
      int(4)
      ["_mark"]=>
      NULL
      }
      }

      My thinking on this is that you are somehow using database calls that
      are causing this to happen. What version of PHP? Let's see the
      news_resource class.

      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Justin Koivisto

        #4
        Re: Arrays and Pointers

        thehuby wrote:
        [color=blue]
        > I have come across an issue using arrays ond objects; I am using an
        > array to keep track of 'n' number of object(The code snippet below is
        > from my initial testing)
        >
        > I first declare the object ($nr = new news_resource() ;), then call a
        > function of the object ($nr->params_init( ); ). This is the put into
        > the first element of the db, I then call the function again and put the
        > object in the next element array.
        >
        > However when I print it all out at the end, all the elements of the
        > array store the object with the same values...[/color]

        Oops, I didn't read it close enough...
        [color=blue]
        > if I redeclare the object ($nr = new news_resource() ; ) again, before
        > calling the funciton then it gets added in fine.
        >
        > I'm sure its a typical behaviour and is to do with pointers - but I
        > don't understand why reassiging the object ($obj = new my_class() )
        > before callings its funciton should break the link to the original
        > object (and maintain its state).
        >
        > In my mind it should either always put a copy in the array or else
        > always be a pointer.
        >
        > Can anyone explain why PHP works this way?
        >
        > Code snippet below:
        >
        > $nr = new news_resource() ;[/color]

        Assume:
        $nr == 0xffff6224
        [color=blue]
        > $nr->params_init( 0, "www.num1.c om", "Link to num1", "This links to
        > number 1", 0, 0 );
        > $resources[] = $nr;[/color]

        $resources[0] == 0xffff6224
        [color=blue]
        > $nr = new news_resource() ; //why does this mean that the code works
        > and the array keeps a copy of the object instead of a pointer?[/color]

        If you do the above line:
        $nr == 0xffff65a4

        Otherwise:
        $nr == 0xffff6224
        [color=blue]
        > $nr->params_init( 0, "www.num2.c om", "Link to num2", "This links to
        > number 2", 0, 0 );
        > $resources[] = $nr;[/color]

        If you reset $nr with new:
        $nr == 0xffff6c40

        else:
        $resources[1] == 0xffff6224
        [color=blue]
        > $nr = new news_resource() ;
        > $nr->params_init( 0, "www.num3.c om", "Link to num3", "This links to
        > number 3", 0, 0 );
        > $resources[] = $nr;[/color]

        If you reset $nr with new:
        $nr == 0xffff7cc2

        else:
        $resources[2] == 0xffff6224

        So now you have 2 possible outputs:

        not resetting $nr each time:
        array(
        0xffff6224
        0xffff6224
        0xffff6224
        0xffff6224
        );

        Resetting $nr using "new" each time:
        array(
        0xffff6224
        0xffff65a4
        0xffff6c40
        0xffff7cc2
        );

        Look at the above numbers assuming they are memory addresses... do you
        see why it behaves that way?

        --
        Justin Koivisto, ZCE - justin@koivi.co m

        Comment

        • thehuby

          #5
          Re: Arrays and Pointers

          I do see why it behaves that way...my issue is that logically my brain
          says it shouldn't. I guess I just don't get why redeclaring the object
          creates a second object with a new pointer for the next array element.

          I think I was having a moan cos I had spent too much time on this
          issue!

          Thanks for the info though..it does clear up my understanding.

          Regards,

          Rick

          Comment

          Working...