Adding a custom object to an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Areric

    Adding a custom object to an array

    Hey all,

    Anyone have any idea what im doing wrong here:

    $image = new Image();
    $image->SetImageConten t($imageRow['imageContent']);
    $image->SetFileSize($i mageRow['fileSize']);
    $image->SetFileName($i mageRow['fileName']);
    $image->SetImageId($im ageRow['imageId']);
    $image->SetParentImage Id($imageRow['parentId']);
    $image->SetFileType($i mageRow['fileType']);
    $image->SetImageHeight ($imageRow['height']);
    $image->SetImageWidth( $imageRow['width']);
    $image->SetIsThumbnail ($imageRow['isThumb']);
    $this->mImages[$this->mImageCount] = $image;

    where Image is a custom class and mImages is an array declared as
    follows:
    $this->mImages = array();

    Whats happening is im executing this function
    function GetImage($image Index)
    {
    return $this->mImages[$imageIndex];

    }

    and then trying to call a member function on the returned value
    i.e.
    $image = $album->GetImage(1);
    $imageId = $image->GetImageId() ;

    and im getting this error" Fatal error: Call to a member function on a
    non-object in
    /home/flyerweb/public_html/ImageManipulato r/POCPages/DisplayAllImage sInAlbum.php
    on line 30"

    Any idea why i cant add a custom object to an array then pull it back
    out and call a method?

  • Rik

    #2
    Re: Adding a custom object to an array

    Areric wrote:[color=blue]
    > Any idea why i cant add a custom object to an array then pull it back
    > out and call a method?[/color]

    <?php

    class A{
    var $elements = array();
    var $count;
    function A(){
    $this->count = 0;
    }
    function add_element($te xt){
    $object = new B($text);
    $this->elements[$this->count] = $object;
    $this->count++;
    }
    function return_element( $int){
    return $this->elements[$int];
    }
    }
    class B{
    var $text;
    function B($var){
    $this->text = $var;
    }
    function display(){
    echo $this->text;
    }
    }

    $test = new A();
    $test->add_element("T his works");
    $test->add_element("S everal times even");
    $object = $test->return_element (0);
    $object2 = $test->return_element (1);
    $object->display();
    $object2->display();
    ?>


    Outputs: "This worksSeveral times even"

    So, I think the problem would be elsewhere.
    What do you see if you var_dump() your $album, or print_r($album->mImages),
    before getting the image out? Does it really contain the object?

    Maybe the Image class is only declared after the album has already tried to
    add the images?

    What happens if in the method you add the images, instead of
    $this->mImages[$this->mImageCount] = $image;
    return is_object($imag e):

    Does it return false or true?


    Grtz,
    --
    Rik Wasmus


    Comment

    • Areric

      #3
      Re: Adding a custom object to an array

      Rik

      THanks for the help you were right the problem was elsewhere. For some
      reason my mImageCount wasnt getting initalized properly. So when i
      added my first image it was adding it to a index with no value, where
      when i tried to reference it i was referencing index 0. The weird thing
      was i had the mImageCount = 0 in the constructor.

      Oh well i fixed it by initalizing the variable where it was declared
      instead of the constructor.

      Comment

      • Rik

        #4
        Re: Adding a custom object to an array

        Areric wrote:[color=blue]
        > Rik
        >
        > THanks for the help you were right the problem was elsewhere. For some
        > reason my mImageCount wasnt getting initalized properly. So when i
        > added my first image it was adding it to a index with no value, where
        > when i tried to reference it i was referencing index 0. The weird
        > thing was i had the mImageCount = 0 in the constructor.[/color]

        Euhm, you've lost me.
        If you initialize mImageCount = 0;
        Add an object by $this->mImages[$this->mImageCount] = $image;
        Surely it would be at index 0....
        Or were you incrementing mImageCount beforehand?

        Grtz,
        --
        Rik Wasmus


        Comment

        Working...