我是
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,1]
PHP致命错误:在第48行的/home/vooders/public_html/gameReader.PHP中调用未定义的函数setproperty()
编辑:
所以在尝试重命名getter / setter之后我仍然会遇到致命的错误,所以现在我确定它是如何调用这些对象的.
我会试着通过我的想法来谈谈你
$classes [$heroClass] – > {upOne($ladderWinsCoin)};
如果我们在上面这一行,$heroClass将是这个例子’Mage’中来自数据库的字符串.
解决方法
//it should work Now <? class emptyClass { private $ladderWinsNoCoin = 5; 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 setProperty($property,$value) { if (property_exists($this,$property)) { $this->$property = $value; } } public function upOne($property) { if (property_exists($this,$property)) { $this->$property++; } } } $classes = array( 'Druids' => new emptyClass,'Elfos' => new emptyClass ); $classes["Druids"]->setProperty("ladderWinsNoCoin",50); echo $classes["Druids"]->getProperty("ladderWinsNoCoin") . "<br>"; $classes["Druids"]->upOne("ladderWinsNoCoin"); echo $classes["Druids"]->getProperty("ladderWinsNoCoin"). "<br>"; $classes["Elfos"]->setProperty("ladderLossesCoin",25); echo $classes["Elfos"]->getProperty("ladderLossesCoin"). "<br>"; $classes["Elfos"]->upOne("ladderLossesCoin"); echo $classes["Elfos"]->getProperty("ladderLossesCoin"). "<br>"; //50 //51 //25 //26 ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。