开发者

PHP access object property through constant's value

开发者 https://www.devze.com 2023-04-01 07:51 出处:网络
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 shuff

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

精彩评论

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