设置数组对象属性的键
就在今天,我注意到一个对象模型中有一个奇怪的行为,该模型之前工作得很好(我已经检查了所有可能的内容,并且其配置没有任何改变,所以我怀疑 PHP 版本发生了变化,并想知道是否有其他人经历过类似的事情)
直到最近,我还可以手动设置数组对象属性的键。在我的一个模型中,这一点的具体实现包含在一个画廊类中,如下所示:
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;
}
省略了不相关的部分。
基本上,我可以将图库照片对象的数组键设置为数据库中单个照片的 id。这使得单独迭代的编码变得更容易,并使整个事情运行得更顺畅。
现在,无论我将该键设置为什么,foreach 运行时都会自动生成整数。我什至尝试在其中输入一个文字字符串,理论上它应该替换每次迭代,但我仍然得到了属性 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
)
)
在某个次要版本中删除了在作为数组的对象属性中手动设置键的功能,而我不知道这一点吗?我用谷歌搜索了一遍,浏览了 PHP 手册网站,但没有找到答案。有人经历过类似的事情吗?我应该考虑更好的方法吗?我只是真的这么做了,因为它使得通过ajax请求返回下一个逻辑id来实现下一个/上一个系统变得更加容易(记住id可以在之间删除!)
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为你的行为有什么问题,而且我从未经历过你所描述的行为。然而,一个快速的解决方案可能是将赋值行替换为如下内容:
您还可以
echo $gp->GalleryPhotoID;
以确保GalleryPhotoID
属性实际上可以以这种方式访问。最后,您说您用类似以下内容替换了上面的行:
它仍然为每个条目创建了一个带有整数键的新条目?如果是这种情况,那么我认为您省略的代码中可能有某些内容导致了问题。
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:
You could also
echo $gp->GalleryPhotoID;
to ensure that theGalleryPhotoID
property can actually be accessed that way.Lastly, you said you replaced the above line with something akin to:
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.
全程捂脸。新年的臭气一定还在我的脑子里,否则我会注意到,如果没有设置AlbumCover属性的照片,我添加的获取专辑封面缩略图的函数会打乱数组!
我对此进行了修改,仅制作变量的本地副本,并在未设置封面的情况下对其进行洗牌。
我接受并赞成答案和发布的评论,因为您的警告导致我犯了错误。谢谢你们!
Facepalm all the way. The effluvium of New Year's must still be in my brain, else I would have noticed that the function I added to fetch the album cover thumbnail shuffled the array if there wasn't a photo with the AlbumCover property set!
I amended this to just make a local copy of the variable and shuffle that instead if no cover is set.
I accepted and upvoted both the answer and the comment posted since your caveats lead me to my error. Thanks guys!