Search code examples
phpobject-properties

Setting the key of an object property that is an array


Just today I noticed a strange behavior in an object model that was previously working just fine (I have checked everything possible and nothing about its configuration has changed, so I am suspecting a change to PHP version and wondering if anyone else has experience anything similar)

Until recently, I could set the keys of object properties that were arrays manually. The specific implememation of this in one of my models was contained in a gallery class that looked like this:

public function __construct($gid){
        parent::__construct($gid);
        $this->Photos = $this->getPhotos();
        $this->AlbumCover = $this->getCover();
    }

    public function getPhotos(){
        $sql = 'SELECT GalleryPhotoID FROM GalleryPhoto WHERE GalleryID = ?';
        $params = array($this->GalleryID);
        $allids = DatabaseHandler::GetAll($sql, $params);
        $output = array();
        foreach($allids as $id){
            $gp = new GalleryPhoto($id['GalleryPhotoID']);
            $output[$gp->GalleryPhotoID] = $gp;
        }
        return $output;
    }

Irrelevant parts omitted.

Basically, I could set the array keys of the Gallery's Photos object to the individual photo's id in the database. This just made it easier to code for individual iteration and made the whole thing run smoother.

Now, no matter what I set that key to, automatic integers are generated when the foreach runs. I even tried typing a literal string in there, which theoretically should replace every iteration, but I still got incremented, automatic integers for the keys of the property Photos.

[Photos] => Array
        (
            [0] => GalleryPhoto Object
                (
                    [GalleryID] => 9
                    [Caption] => 
                    [Orientation] => 0
                    [AlbumCover] => 
                    [DateAdded] => 2011-01-03 16:58:51
                    [GalleryPhotoID] => 63
                    [Thumbnail] => 
                    [Image] => 
                    [src] => http://..com/galleryImage/getImage/63
                )

            [1] => GalleryPhoto Object
                (
                    [GalleryID] => 9
                    [Caption] => 
                    [Orientation] => 0
                    [AlbumCover] => 
                    [DateAdded] => 2011-01-03 16:58:51
                    [GalleryPhotoID] => 64
                    [Thumbnail] => 
                    [Image] => 
                    [src] => http://..com/galleryImage/getImage/64
                )

        )

Has the abillity to manually set keys within an object property that is an array been removed in some minor release and I am unaware of it? I have googled all over, looked through the PHP manual site and found no answer. Has anyone experienced anything similar? Is there a better approach I should consider? I only really went with this because it made it so much easier to implement a next/previous system via ajax requests back to the next logical id (keeping in mind that ids can be deleted between!)

Thanks!


Solution

  • I don't see anything wrong with what you have, and I've never experienced the behavior you describe. However, a quick solution could be to replace the assignment line with something like this:

    $output[$id['GalleryPhotoID']] = $gp;
    

    You could also echo $gp->GalleryPhotoID; to ensure that the GalleryPhotoID property can actually be accessed that way.

    Lastly, you said you replaced the above line with something akin to:

    $output['foobar'] = $gp;
    

    and it still created a new entry, with integer keys, for each entry? If that's the case, then I think there may be something in the code you omitted that's causing the problem.