1

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!

4

2 回答 2

0

我认为您所拥有的没有任何问题,而且我从未经历过您描述的行为。但是,一个快速的解决方案可能是将分配行替换为以下内容:

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

您还echo $gp->GalleryPhotoID;可以确保GalleryPhotoID可以通过这种方式实际访问该属性。

最后,您说您将上面的行替换为类似于:

$output['foobar'] = $gp;

它仍然为每个条目创建一个带有整数键的新条目?如果是这种情况,那么我认为您省略的代码中可能存在导致问题的内容。

于 2011-01-03T22:45:52.773 回答
0

全程捂脸。新年的气息一定还在我的脑海中,否则我会注意到,如果没有设置了 AlbumCover 属性的照片,我添加的用于获取专辑封面缩略图的函数会打乱数组!

private function getCover(){
        foreach($this->Photos as $ind=>$p){
            if($p->AlbumCover){
                return $this->Photos[$ind];
            }
        }

        shuffle($this->Photos); //this is the problem
        return current($this->Photos);

    }

我将其修改为仅制作变量的本地副本并改组,如果没有设置封面。

private function getCover(){
        foreach($this->Photos as $ind=>$p){
            if($p->AlbumCover){
                return $this->Photos[$ind];
            }
        }
        $Photos = $this->Photos;
        shuffle($Photos);
        return current($Photos);

    }

我接受并赞成答案和发表的评论,因为您的警告导致我犯了错误。多谢你们!

于 2011-01-03T22:58:44.220 回答