By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Can Objects be stored in Arrays and still be accessed ?

P: n/a
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 />' ;
}

?>
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Matthias Scheller wrote:
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;
???
Where does $ob->a[1] point to?

this is your classdefinition:

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

$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 />' ;
}

?>


Jul 17 '05 #2

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


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
--
Jul 17 '05 #3

P: n/a
"Matthias Scheller" <sc******@student.ethz.ch> wrote in message
news:cc**********@newshispeed.ch...
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? -snip- class B {
//for example a form
var $b=array();
}//class -snip- $ob = new B;
$ob->a[1]=new A;
$ob->a[2]=new A;


class b doesn't have a member called a.
Jul 17 '05 #4

P: n/a
Matthias Scheller schrieb:
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?

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($ob->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 />' ;
}
?>
Jul 17 '05 #5

P: n/a
Matthias Scheller a écrit :
Matthias Scheller schrieb:
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?


Thanks to you all!

The point really was the foreach()-construct, which works on a copy of
the array!


Note that you can still alter the array :

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

Bruno
Jul 17 '05 #6

P: n/a
"Matthias Scheller" <sc******@student.ethz.ch> wrote in message
news:cc**********@newshispeed.ch...
Matthias Scheller schrieb:
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?

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...


The most efficient (and less confusing) way to operate on an array is to use
array_walk().
Jul 17 '05 #7

P: n/a
yes
--
Jeffrey Silverman
je*****@pantsjhu.edu
Drop "pants" to reply by email

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.