开发者

dynamic property call

开发者 https://www.devze.com 2023-03-02 22:26 出处:网络
is there a shorter way to achieve the following: $i = 1; $p开发者_StackOverflowrop = \'image\' . $i;

is there a shorter way to achieve the following:

$i = 1;
$p开发者_StackOverflowrop = 'image' . $i;
$image = $this->getObject->$prop;

I have in mind there was something like

$image = $this->getObject->image{$i};

any ideas?


How about:

$this->getObject->{'image'.$i};


A better solution would be to make an array of all images, and then call it like this:

$this->getObject->image[$i]

This will be faster, and is often much easier to work with than a bunch of different variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消