gpt4 book ai didi

php - 从数组访问类变量

转载 作者:可可西里 更新时间:2023-10-31 23:19:07 27 4
gpt4 key购买 nike

我是 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]

当它运行时我得到一个 fatal error

PHP fatal error :调用/home/vooders/public_html/gameReader.php 中的未定义函数 setProperty() 第 48 行


编辑:

所以在尝试重命名 getter/setter 之后我仍然遇到 fatal error ,所以现在我确定它是如何调用对象的。

我会试着告诉你我的想法

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

如果我们采用上面的这一行,$heroClass 将是本例中数据库中的一个字符串 'Mage'

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

最佳答案

明确地说,我会建议您不要“在了解类业务的类外部编写代码。”

“我是一个类。因此,我还活着。告诉我发生了什么,我会做出相应的回应。问我你想知道什么我会为你提供答案。但是:不要插手类事务,因为你很脆,加上伍斯特沙司味道很好!!”

(1) 不要“从外部”对类的变量施加粘手。 告诉类发生了什么,它可能会增加或减少自己的属性。 (你确实说它们是私有(private)的,不是吗?他们应该是。)

(2) 如果您想知道“答案”,它可能基于一个或多个属性的值,根据简单或复杂的逻辑,那么应该包含该逻辑,因为它属于“自身”。

关于php - 从数组访问类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31212439/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com