Professional Web Applications Themes

How to store objects in array and the retrieve them - PHP Development

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

  1. #1

    Default How to store objects in array and the retrieve them

    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.
    J.P. Guest

  2. #2

    Default Re: How to store objects in array and the retrieve them

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

    --
    [url]http://www.dbForumz.com/[/url] This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: [url]http://www.dbForumz.com/PHP-store-objects-array-retrieve-ftopict141572.html[/url]
    Visit Topic URL to contact author (reg. req'd). Report abuse: [url]http://www.dbForumz.com/eform.php?p=473383[/url]
    steve Guest

  3. #3

    Default Re: How to store objects in array and the retrieve them

    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: [url]http://www.php.net/references[/url]

    -- brion vibber (brion pobox.com)
    Brion Vibber Guest

  4. #4

    Default Re: How to store objects in array and the retrieve them

    Brion Vibber wrote:
    > 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: [url]http://www.php.net/references[/url]
    >
    > -- 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. Guest

  5. #5

    Default Re: How to store objects in array and the retrieve them

    steve wrote:
    > "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).
    >
    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.
    J.P. Guest

Similar Threads

  1. store/retrieve images to server
    By Suren_kancherla in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: April 11th, 07:02 AM
  2. Insert and Retrieve binary objects with MS-SQL 2000
    By HeloWorld in forum Coldfusion Database Access
    Replies: 4
    Last Post: July 22nd, 12:29 PM
  3. store and retrieve flash in and from database
    By kulderzipke in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: March 31st, 02:48 PM
  4. Retrieve Client Certificate using COM Objects
    By Edugtpa in forum ASP Components
    Replies: 0
    Last Post: March 30th, 06:03 AM
  5. What is this objects name in the array?!
    By equinox007 webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: January 13th, 06:47 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139