Php 设置作为数组的对象属性的键

Php 设置作为数组的对象属性的键,php,object-properties,Php,Object Properties,就在今天,我注意到一个对象模型中有一个奇怪的行为,它以前工作得很好(我已经检查了所有可能的东西,但它的配置没有任何变化,所以我怀疑PHP版本有变化,不知道是否有其他人有过类似的经历) 直到最近,我还可以手动设置数组中的对象属性键。在我的一个模型中,这一点的具体实现包含在一个gallery类中,看起来如下所示: public function __construct($gid){ parent::__construct($gid); $this->Photos

就在今天,我注意到一个对象模型中有一个奇怪的行为,它以前工作得很好(我已经检查了所有可能的东西,但它的配置没有任何变化,所以我怀疑PHP版本有变化,不知道是否有其他人有过类似的经历)

直到最近,我还可以手动设置数组中的对象属性键。在我的一个模型中,这一点的具体实现包含在一个gallery类中,看起来如下所示:

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;
    }
$output[$id['GalleryPhotoID']] = $gp;
不相关的部分省略了

基本上,我可以将Gallery的Photos对象的数组键设置为数据库中单个照片的id。这使得为单个迭代编写代码变得更容易,并使整个过程运行更顺畅

现在,无论我将该键设置为什么,当foreach运行时都会生成自动整数。我甚至试着在那里输入一个文本字符串,理论上应该替换每次迭代,但我仍然得到了属性照片键的递增的自动整数

[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
                )

        )
在数组的对象属性中手动设置键的功能是否在某个小版本中被删除,而我不知道?我在谷歌上搜索了所有地方,浏览了PHP手册网站,没有找到答案。有没有人经历过类似的事情?有更好的方法吗?我之所以这么做是因为它使得通过ajax请求返回下一个逻辑id来实现下一个/上一个系统变得更加容易(请记住,id可以在两个逻辑id之间删除!)


谢谢

我看不出你所做的有什么不对,我也从未经历过你所描述的行为。但是,一个快速的解决方案可能是用以下内容替换分配行:

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;
    }
$output[$id['GalleryPhotoID']] = $gp;
您还可以
echo$gp->GalleryPhotoID
以确保实际可以通过这种方式访问GalleryPhotoID属性

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

$output['foobar'] = $gp;

它仍然为每个条目创建了一个新条目,带有整数键?如果是这样的话,那么我认为您忽略的代码中可能有什么东西导致了问题。

Facepalm。新年的臭味一定还在我的脑子里,否则我会注意到,如果没有设置了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);

    }

我接受并更新了答案和发表的评论,因为你的警告导致了我的错误。谢谢大家

没有引入此类限制;我以前见过这种情况,它总是归结为调用堆栈中的某个模糊的地方。你能再发一些代码吗?