从数组中访问类变量

时间:2015-07-03 18:21:32

标签: php

我是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, 'Mage', 'Druid', 1, 0, 1]

当它运行时我会收到致命错误

PHP Fatal error: Call to undefined function setProperty() in /home/vooders/public_html/gameReader.php on line 48

编辑:

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

我会试着通过我的思考来谈谈你

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

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

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

2 个答案:

答案 0 :(得分:2)

分类地说,我建议你不要“在一个知道班级业务的班级外部编写代码。”

“我是班级。因此,我活着。告诉我发生了什么,我会做出相应的回应。问我想知道什么我将为你提供答案。但是:不要干涉课堂的事务,因为你很脆脆,用伍斯特郡酱很好吃!!“

(1)不要“从外面”把手指粘在班级的变量上。 告诉班级发生了什么,它可以增加或减少自己的属性。 (你确实说他们是private,不是吗?他们应该这样做。)

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

答案 1 :(得分:1)

//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
?>