4

I'm new to PHP and I'm having trouble accessing some class variables.

I have this class:

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;
        }
    }
}

I then create 9 instances of it in an array

/* 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
);  

Next I want to increment the values of the class variables to match data got from a database and this is what I've come up with

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)};
    }

Where $game is an array from the database that looks something like this

[1, 'Mage', 'Druid', 1, 0, 1]

When it runs I get a fatal error

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


Edit:

So after trying renaming the getter/setter I'm still getting the fatal error, so now I'm sure its how I'm calling the objects.

I'll try to talk you through my thinking

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

If we take this line above, $heroClass will be a string from the database in this example 'Mage'.

Now I want to use this string to call the right object from the $classes array then increment the appropriate variable by 1.

2
  • 4
    __set and __get are magic methods. According to the docs - You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.. You explicitly calling them, could be wrong. Try renaming them to something else. Commented Jul 3, 2015 at 18:26
  • Also this might be useful for handling multiple properties you are dealing with. Commented Jul 3, 2015 at 18:57

2 Answers 2

2

Categorically, I would advise you not to "write code external to a class that knows the class's business."

"I am a class. Therefore, I am alive. Tell me what has happened and I shall respond accordingly. Ask me what you want to know and I shall provide you with the answer. But: Do Not Meddle in the Affairs of Classes, for you are crunchy and taste good with worcestershire sauce!!"

(1) Don't put your sticky fingers on the class's variables "from outside." Tell the Class what has happened, that it may increment or decrement its own properties. (You did say they were private, didn't you? As they should be.)

(2) If you want to know "an answer," which may be based on the value of one or many properties, according to simple or complex logic, then the Class should contain that logic, as it pertains "to itself."

Sign up to request clarification or add additional context in comments.

1 Comment

cheers for that you are right! I have made a class function to increment the value. Not sure why I didn't to start with, but I'm still having the same problem with calling the object to start with.
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
?>

2 Comments

Tried renaming the functions with the same effect so I'm calling the objects wrong I think
Thanks a lot! That was what i was doing wrong i was passing the variables like $ladderWinsCoin rather than as strings 'ladderWinsNoCoin' thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.