开发者

PHP access object property through constant's value

I have a Player class which has properties: $infantry, $vehicles and $air.

When battling players, I don't know which property is being used as an array which holds the properties to be used is shuffled to create a random order.

I try to use this, but it doesn't work. Strangely it doesn't give me empty property error so I assume it's pointing to some property:

<?php

$typeOrder = array(_INF_, _VEH_, _AIR_); // _INF_ const = "infantry" etc
$turnOrder = $typeOrder;
shuffle($turnOrder);
for($i = 0; $i < 3; $i++)
{
   $attType = $turnOrder[$i];
   print $p1->$attType;
}

?>

How do I properly access a property with the开发者_如何学C value held in a constant?

Thanks.


It should work - as long as $p1->infantry etc exist. What error does PHP give you exactly?

By the way, have a look at array_rand().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜