php类中 数组变量,php – 从数组中访问类变量
发布日期:2021-06-24 11:49:21 浏览次数:2 分类:技术文章

本文共 2681 字,大约阅读时间需要 8 分钟。

我是

PHP的新手,我无法访问一些类变量.

我有这门课:

class emptyClass

{

private $ladderWinsNoCoin = 0;

private $ladderWinsCoin = 0;

private $arenaWinsNoCoin = 0;

private $arenaWinsCoin = 0;

private $ladderLossesNoCoin = 0;

private $ladderLossesCoin = 0;

private $arenaLossesNoCoin = 0;

private $arenaLossesCoin = 0;

public function getProperty($property) {

if (property_exists($this, $property)) {

return $this->$property;

}

}

public function upOne($property) {

if (property_exists($this, $property)) {

$this->$property=($property+1);

}

}

public function setProperty($property, $value) {

if (property_exists($this, $property)) {

$this->$property = $value;

}

}

}

然后我在一个数组中创建它的9个实例

/* Create classes in an array so we can call

them with strings from the database*/

$classes = array(

'Druid' => new emptyClass,

'Hunter' => new emptyClass,

'Mage' => new emptyClass,

'Paladin'=> new emptyClass,

'Priest' => new emptyClass,

'Rogue' => new emptyClass,

'Shaman' => new emptyClass,

'Warlock'=> new emptyClass,

'Warrior'=> new emptyClass

);

接下来我想增加类变量的值以匹配从数据库获得的数据,这就是我想出的

foreach ($games as $game){ // Go through the games array from the database

$gameID = current($game); // Unused (for now)

$heroClass = (string)next($game);

$villainClass = (string)next($game); // Unused (for now)

$win = (int)next($game);

$coin = (int)next($game);

$ladder = (int)next($game);

//Add up the values in the class objects

if ($ladder==1&&$coin==1&&$win==1){ // Ladder win with coin

$classes[$heroClass] -> {upOne($ladderWinsCoin)};

} else if ($ladder==1&&$coin==1&&$win==0){ // Ladder loss with coin

$classes[$heroClass] -> {upOne($ladderLossesCoin)};

} else if ($ladder==1&&$coin==0&&$win==1){ // Ladder win without coin

$classes[$heroClass] -> {upOne($ladderWinsNoCoin)};

} else if ($ladder==1&&$coin==0&&$win==0){ // Ladder loss without coin

$classes[$heroClass] -> {upOne($ladderLossesNoCoin)};

} else if ($ladder==0&&$coin==1&&$win==1){ // Arena win with coin

$classes[$heroClass] -> {upOne($arenaLossesCoin)};

} else if ($ladder==0&&$coin==1&&$win==0){ // Arena loss with coin

$classes[$heroClass] -> {upOne($arenaLossesCoin)};

} else if ($ladder==0&&$coin==0&&$win==1){ // Arena win without coin

$classes[$heroClass] -> {upOne($arenaWinsNoCoin)};

} else if ($ladder==0&&$coin==0&&$win==0){ // Arena loss without coin

$classes[$heroClass] -> {upOne($arenaLossesNoCoin)};

}

$game是数据库中的数组,看起来像这样

[1,’法师’,’德鲁伊’,1,0,1]

当它运行时,我得到一个致命的错误

PHP致命错误:在第48行的/home/vooders/public_html/gameReader.php中调用未定义的函数setProperty()

编辑:

所以在尝试重命名getter / setter之后我仍然会遇到致命的错误,所以现在我确定它是如何调用这些对象的.

我会试着通过我的想法来谈谈你

$classes [$heroClass] – > {upOne($ladderWinsCoin)};

如果我们在上面这一行,$heroClass将是这个例子’Mage’中来自数据库的字符串.

现在我想使用这个字符串从$classes数组中调用正确的对象,然后将适当的变量增加1.

转载地址:https://blog.csdn.net/weixin_32573931/article/details/116249725 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Php判断二中二算法中奖,PHP+MySQL应用中使用XOR运算加密算法(2)
下一篇:php打开vue项目空白,如何解决Vue 2.0在IE11中打开项目页面空白的问题

发表评论

最新留言

很好
[***.229.124.182]2024年04月15日 18时24分25秒