For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > August 2004 > How to store objects in array and the retrieve them









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to store objects in array and the retrieve them
J.P.

2004-08-19, 9:11 pm

Hi,

I'm trying to store objects in an array and then later I want to
retrieve the object again. I tried it the following way, but it doesn't
work.

$items = array();
$newItem = new Item();
array_push($items, $newitem);

$retrievedItem = $items[0];
$retrievedItem->display();

Now I get the message:

Fatal error: Call to undefined function: display() in ...

So $retrievedItem is not of class Item. How can I retrieve it so it is
an object of class Item?

J.P.
steve

2004-08-20, 4:33 am

"J.P." wrote:
> Hi,
>
> I’m trying to store objects in an array and then later I want to
>
> retrieve the object again. I tried it the following way, but it
> doesn’t
> work.
>
> $items = array();
> $newItem = new Item();
> array_push($items, $newitem);
>
> $retrievedItem = $items[0];
> $retrievedItem->display();
>
> Now I get the message:
>
> Fatal error: Call to undefined function: display() in ...
>
> So $retrievedItem is not of class Item. How can I retrieve it so it

is
>
> an object of class Item?
>
> J.P.


There may be a better solution, but you can always serialize the
object (see php doc).

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-store-o...pict141572.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=473383
Brion Vibber

2004-08-20, 4:33 am

J.P. wrote:
> I'm trying to store objects in an array and then later I want to
> retrieve the object again. I tried it the following way, but it doesn't
> work.
>
> $items = array();
> $newItem = new Item();
> array_push($items, $newitem);


If this is your exact code, it's going to fail because variable names
are case sensitive. You're pushing an uninitialized variable, hence a
NULL value.

When copying objects around you may also want to make sure you
understand the awful issue of references: http://www.php.net/references

-- brion vibber (brion @ pobox.com)
J.P.

2004-08-20, 3:56 pm

Brion Vibber wrote:
> J.P. wrote:
>
>
>
> If this is your exact code, it's going to fail because variable names
> are case sensitive. You're pushing an uninitialized variable, hence a
> NULL value.
>
> When copying objects around you may also want to make sure you
> understand the awful issue of references: http://www.php.net/references
>
> -- brion vibber (brion @ pobox.com)


No, this isn't my exact code. I'm quite new to PHP, but not to
programming. But I figured my problem out. Programming OOP in PHP is
slightly different than in p.e. Java.
Thanks anyway.

J.P.
J.P.

2004-08-20, 3:56 pm

steve wrote:

> "J.P." wrote:
> is
>
> There may be a better solution, but you can always serialize the
> object (see php doc).
>


Serialization is indeed an option, but I could not use it here. But
after a few hours i figured it out. The problem was somewhere else.
Thanks anyway.

J.P.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com