Sharpen the Battle Result with a Class

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:05
    The most obvious time you should create a class
  • 0:08
    is when you're passing around an associative array of data.
  • 0:11
    Check out the battle function.
  • 0:13
    It returns an associative array with 'winning_ship,'
  • 0:17
    'losing_ship,' and 'used_jedi_powers' keys.
  • 0:21
    We use this in battle.php.
  • 0:24
    Set it to an $outcome variable.
  • 0:26
    Then reference all those keys to print stuff further down.
  • 0:32
    Oh man, I hate this kind of stuff.
  • 0:35
    It's not obvious at all what's inside the $outcome variable
Loading ...

Sharpen the Battle Result with a Class


The next thing we want to tackle is our battle() function, with its array, inside our BattleManager class. It's not obvious at all what's inside the $outcome variable, or whether the keys it has now might be missing or different in the future. To rectify this we're going to create a new BattleResult model class with some properties and methods that will clean things up and remove the need for weird associative arrays. Our BattleManager::battle() function will return a nice BattleResult object, and we'll be in full control of what public methods we put on that.

Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.