473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

?>
Jul 17 '05 #1
7 1462
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
*** 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
"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
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
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
"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
yes
--
Jeffrey Silverman
je*****@pantsjhu.edu
Drop "pants" to reply by email

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Paul Rubin | last post by:
I've had this recurring half-baked desire for long enough that I thought I'd post about it, even though I don't have any concrete proposals and the whole idea is fraught with hazards. Basically...
2
by: don | last post by:
My question is, do C++ array of objects hold the objects or just the pointers to the objects..... I know Java arrays only hold pointers to objects, but I seem to remember that C++ arrays hold the...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
11
by: CSN | last post by:
Is it possible to iterate over an array in plpgsql? Something like: function insert_stuff (rel_ids int) .... foreach rel_ids as id insert into table (rel_id, val) values (id, 5);
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
15
by: Marc Thrun | last post by:
Hello, I've got a few questions: 1) Given the two structs struct A { int x; }; and
2
by: headware | last post by:
I am new to 3-tiered design and am in the process of designing an application that will work with ASP.NET as well as Windows Forms (and possibly with a PDA of some sort down the road). My question...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.