Re: Function returns an array of objects...
| From: | Tom Rogers | Date: | Fri, 28 Feb 2003 07:13:06 +0000 |
| Subject: | Re: Function returns an array of objects... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-137523@lists.php.net to get a copy of this message | ||
Hi,
Friday, February 28, 2003, 4:24:54 PM, you wrote:
AS> so i have a function that creates objects (with member variables) and
AS> pushes those objects onto the array. after adding all of the objects,
AS> the function then returns the array...
AS> i'm having trouble accessing the members of each object in the
AS> array...i'm trying to do something like this:
AS> class Object {
AS> var $id;
AS> function Object($param) {
AS> $this->id = $param;
AS> }
AS> }
AS> function getArrayOfObjects() {
AS> $theArray = array();
AS> for ($i = 0; $i < 10; $i++) {
AS> $theArray[] = new Objects($i);
AS> }
AS> return $theArray;
AS> }
AS> $temp = getArrayOfObjects();
echo $temp[0]->>id; // this should display "0"
AS> do i have to do something with references or dereference the id
AS> variable some other way? i'm coming from java where something like this
AS> is extremely easy...
AS> thanks
AS> amit
Try like this (note the 2 & symbols)
function &getArrayOfObjects() {
$theArray = array();
for ($i = 0; $i < 10; $i++) {
$theArray[] =& new Objects($i);
}
return $theArray;
}
$temp = getArrayOfObjects();
echo $temp[0]->id;
--
regards,
Tom