Can Objects be stored in Arrays and still be accessed ?

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

    Can Objects be stored in Arrays and still be accessed ?

    In order to have a form modeled, I would like to store the objects,
    which represent the form fields in an array of the object representing
    the form.

    Nevertheless, the values of the objects in the array can be sometimes
    accessed, sometimes not. Any explanations?

    Here is a similar code:

    <?php
    class A {
    //for example a form field
    var $a;

    function set_a($arg){
    $this->a=$arg;
    }//function
    }//class

    class B {
    //for example a form
    var $b=array();
    }//class


    /*
    --here the script Output:

    1: 1
    2: 2

    1:
    2:

    */

    $ob = new B;
    $ob->a[1]=new A;
    $ob->a[2]=new A;

    foreach($ob->a as $index=>$value) {
    $value->set_a($index );
    echo $index.': '.$value->a.'<br />' ;
    }

    foreach($ob->a as $index=>$value) {
    echo $index.': '.$value->a.'<br />' ;
    }

    ?>
  • Erwin Moller

    #2
    Re: Can Objects be stored in Arrays and still be accessed ?

    Matthias Scheller wrote:
    [color=blue]
    > In order to have a form modeled, I would like to store the objects,
    > which represent the form fields in an array of the object representing
    > the form.
    >
    > Nevertheless, the values of the objects in the array can be sometimes
    > accessed, sometimes not. Any explanations?
    >
    > Here is a similar code:
    >
    > <?php
    > class A {
    > //for example a form field
    > var $a;
    >
    > function set_a($arg){
    > $this->a=$arg;
    > }//function
    > }//class
    >
    > class B {
    > //for example a form
    > var $b=array();
    > }//class
    >
    >
    > /*
    > --here the script Output:
    >
    > 1: 1
    > 2: 2
    >
    > 1:
    > 2:
    >
    > */
    >
    > $ob = new B;
    > $ob->a[1]=new A;[/color]

    ???
    Where does $ob->a[1] point to?

    this is your classdefinition :

    class B {
    //for example a form
    var $b=array();
    }//class


    Regards,
    Erwin

    [color=blue]
    > $ob->a[2]=new A;
    >
    > foreach($ob->a as $index=>$value) {
    > $value->set_a($index );
    > echo $index.': '.$value->a.'<br />' ;
    > }
    >
    > foreach($ob->a as $index=>$value) {
    > echo $index.': '.$value->a.'<br />' ;
    > }
    >
    > ?>[/color]

    Comment

    • Alvaro G Vicario

      #3
      Re: Can Objects be stored in Arrays and still be accessed ?

      *** Matthias Scheller wrote/escribió (Thu, 08 Jul 2004 12:21:34 +0200):[color=blue]
      > foreach($ob->a as $index=>$value) {
      > $value->set_a($index );
      > echo $index.': '.$value->a.'<br />' ;
      > }[/color]

      The problem is the foreach() constructor. You are not modifying the
      original object but a copy.


      Manual says:

      Note:
      Also note that foreach operates on a copy of the specified array and not
      the array itself. Therefore, the array pointer is not modified as with the
      each() construct, and changes to the array element returned are not
      reflected in the original array. However, the internal pointer of the
      original array is advanced with the processing of the array. Assuming the
      foreach loop runs to completion, the array's internal pointer will be at
      the end of the array.


      --
      --
      -- Álvaro G. Vicario - Burgos, Spain
      --

      Comment

      • CJ Llewellyn

        #4
        Re: Can Objects be stored in Arrays and still be accessed ?

        "Matthias Scheller" <schellem@stude nt.ethz.ch> wrote in message
        news:ccj72d$p5f $1@newshispeed. ch...[color=blue]
        > In order to have a form modeled, I would like to store the objects,
        > which represent the form fields in an array of the object representing
        > the form.
        >
        > Nevertheless, the values of the objects in the array can be sometimes
        > accessed, sometimes not. Any explanations?[/color]
        -snip-[color=blue]
        > class B {
        > //for example a form
        > var $b=array();
        > }//class[/color]
        -snip-[color=blue]
        > $ob = new B;
        > $ob->a[1]=new A;
        > $ob->a[2]=new A;[/color]

        class b doesn't have a member called a.


        Comment

        • Matthias Scheller

          #5
          Re: Can Objects be stored in Arrays and still be accessed ?

          Matthias Scheller schrieb:
          [color=blue]
          > In order to have a form modeled, I would like to store the objects,
          > which represent the form fields in an array of the object representing
          > the form.
          >
          > Nevertheless, the values of the objects in the array can be sometimes
          > accessed, sometimes not. Any explanations?[/color]


          Thanks to you all!

          The point really was the foreach()-construct, which works on a copy of
          the array!
          The array was also declared as var $b=array() and adressed as $ob->a,
          which helped confusing...

          Below, anybody interested will find code to demonstrate the differennt
          effect of a for-loop and the foreach-construct:


          <?php
          class A {
          //for example a form field
          var $a;

          function set_a($arg){
          $this->a=$arg;
          }//function
          }//class

          class B {
          //for example a form
          var $b=array();
          }//class


          /*
          --here the script Output:

          1: 1
          2: 2

          1:
          2:

          */

          $ob = new B;
          $ob->b[0]=new A;
          $ob->b[1]=new A;

          $size=sizeof($o b->b);
          echo $size.'<br />' ;


          foreach($ob->b as $index=>$value) {
          $value->set_a($index );
          echo $index.': '.$value->a.'<br />' ;
          }

          foreach($ob->b as $index=>$value) {
          echo $index.': '.$value->a.'<br />' ;
          }


          for ($i=0;$i<$size; $i++){
          $ob->b[$i]->set_a($i);
          echo $i.': '.$ob->b[$i]->a.'<br />' ;
          }

          for ($i=0;$i<$size; $i++){
          echo $i.': '.$ob->b[$i]->a.'<br />' ;
          }


          ?>

          Comment

          • bruno modulix

            #6
            Re: Can Objects be stored in Arrays and still be accessed ?

            Matthias Scheller a écrit :[color=blue]
            > Matthias Scheller schrieb:
            >[color=green]
            >> In order to have a form modeled, I would like to store the objects,
            >> which represent the form fields in an array of the object representing
            >> the form.
            >>
            >> Nevertheless, the values of the objects in the array can be sometimes
            >> accessed, sometimes not. Any explanations?[/color]
            >
            >
            >
            > Thanks to you all!
            >
            > The point really was the foreach()-construct, which works on a copy of
            > the array![/color]

            Note that you can still alter the array :

            $a = Array(9, 8, 7, 6)
            foreach ($a as $index=>$value) {
            $a[$index] = $value + 10;
            }

            Bruno

            Comment

            • Chung Leong

              #7
              Re: Can Objects be stored in Arrays and still be accessed ?

              "Matthias Scheller" <schellem@stude nt.ethz.ch> wrote in message
              news:ccjhtd$2iq $1@newshispeed. ch...[color=blue]
              > Matthias Scheller schrieb:
              >[color=green]
              > > In order to have a form modeled, I would like to store the objects,
              > > which represent the form fields in an array of the object representing
              > > the form.
              > >
              > > Nevertheless, the values of the objects in the array can be sometimes
              > > accessed, sometimes not. Any explanations?[/color]
              >
              >
              > Thanks to you all!
              >
              > The point really was the foreach()-construct, which works on a copy of
              > the array!
              > The array was also declared as var $b=array() and adressed as $ob->a,
              > which helped confusing...[/color]

              The most efficient (and less confusing) way to operate on an array is to use
              array_walk().


              Comment

              • Jeffrey Silverman

                #8
                Re: Can Objects be stored in Arrays and still be accessed ?

                yes
                --
                Jeffrey Silverman
                jeffrey@pantsjh u.edu
                Drop "pants" to reply by email

                Comment

                Working...