The Wonder of Class Constants

Video loading...

  • 0:03
    The Wonder of Class Constants Leanna Pelham
  • 0:07
    Hey friends, I'm so glad you're here
  • 0:10
    for Part 4 of Baking Delicious Chocolate Chip Cookies.
  • 0:14
    Oh, wait.
  • 0:16
    They're telling me that's not right.
  • 0:18
    Oh! OK, I'm so glad you're here for Part 4 of our Object-Oriented Programming series.
  • 0:26
    After the first 3 parts, you guys are already dangerous.
  • 0:30
    So I'm impressed you're still showing up and aren't off coding something cool.
  • 0:34
    You made the right choice.
  • 0:37
    In this course, we're going to really have fun
  • 0:39
    with some of the coolest parts of OO,
  • 0:42
    showing off features that we haven't mentioned yet.
  • 0:45
    This is packed with the final pieces that will let you recognize
  • 0:49
    all the different OO things that you see in other people's code.
  • 0:53
    There's lots to get through, so let's go.
  • 0:56
    If you're serious about getting really good at this stuff, code along with me.
  • 1:01
    To do that, download the source code from this page,
  • 1:04
    unzip it and move into the start directory.
  • 1:08
    When you do that, you'll have the same code that I have here.
  • 1:12
    Open up the README file, and follow the instructions inside to get things going.
  • 1:17
    When that's done, open your favorite terminal app.
  • 1:20
    Move into the directory and like we've done in the previous courses,
  • 1:23
    start the built-in PHP web server by running
  • 1:27
    php -S localhost: 8000.
  • 1:31
    This is a great server to use for development.
  • 1:35
    Then in our browser we can go to localhost: 8000.
  • 1:39
    Here is our beautiful battles app.
  • 1:42
    Our users have been clamoring for a new feature.
  • 1:46
    A way to battle that forces Jedi powers to be used or completely avoid it.
  • 1:52
    Let's add this, it'll show off a new cool new thing,
  • 1:55
    class constants.
  • 1:58
    Open index.php and scroll down.
  • 2:02
    Right after the ship select boxes,
  • 2:05
    but before the submit button, I'll paste some HTML for a new select box.
  • 2:11
    Let's refresh and see what it looks like.
  • 2:15
    OK, it's a new drop-down called Battle Type,
  • 2:18
    with options for Normal, No Jedi Powers and Only Jedi Powers.
  • 2:25
    If you look at the code, this is a single select field that has a name of battle_type.
  • 2:31
    Here's the idea, each type will cause the battle manager
  • 2:35
    to battle these 2 ships in slightly different ways.
  • 2:40
    Let's hook this up as simply as possible.
  • 2:42
    Since the field is named battle_type, open battle.php,
  • 2:47
    the file that handles the submit.
  • 2:51
    Right before calling the battle method,
  • 2:53
    create a new variable called $battleType,
  • 2:56
    set to $_POST['battle_type'].
  • 3:03
    Then pass battleType as a new fifth argument to the battle method.
  • 3:08
    Go outside that.
  • 3:10
    Open BattleManager and find battle.
  • 3:13
    Give this a new fifth argument, $battleType.
  • 3:17
    Great.
  • 3:19
    We know that this will be 1 of 3 special strings.
  • 3:22
    Either normal, no_jedi or only_jedi.
  • 3:28
    We can use those to change the behavior.
  • 3:31
    First, the 2 blocks near the top
  • 3:33
    should only be run if Jedi powers are being used.
  • 3:37
    Add to the if statement,
  • 3:39
    if $battleType != 'no_jedi' then we can run this.
  • 3:46
    Copy that and add it to the second block.
  • 3:50
    Perfect.
  • 3:52
    If the $battleType is normal or only_jedi,
  • 3:55
    these blocks will execute.
  • 3:58
    Next, the last 2 lines are when the 2 ships battle each other normally.
  • 4:03
    If we're on only_jedi mode, this shouldn't happen.
  • 4:08
    Surround them with an if statement.
  • 4:10
    If $battleType != 'only_jedi'
  • 4:15
    then run these lines.
  • 4:19
    Awesome.
  • 4:20
    Now there's just 1 little last detail.
  • 4:24
    If 2 ships are fighting in only_jedi mode
  • 4:27
    and both have zero Jedi Powers
  • 4:29
    they'll get caught in this loop and fight forever.
  • 4:33
    To prevent that, above the while,
  • 4:36
    add a new $i=0 variable.
  • 4:41
    Then at the bottom, if $i = 100,
  • 4:45
    we're probably stuck in a loop.
  • 4:47
    Just set $ship1Health = 0 and $ship2Health=0
  • 4:53
    and increment $i below that.
  • 4:57
    Done.
  • 4:59
    Give it a try.
  • 5:01
    Select 1 Jedi Starfighter,
  • 5:05
    1 CloakShape Fighter and choose Only Jedi Powers.
  • 5:11
    Hit Engage and the Jedi StarFighter,
  • 5:14
    used its Jedi Powers for a stunning victory!
  • 5:18
    If we refresh, 1 of the ships will use its Jedi Powers every single time.
  • 5:27
    Feature complete, and it was easy.
  • 5:31
    So what's the problem?
  • 5:34
    Look at these strings,
  • 5:36
    "normal", "no_jedi", and "only_jedi".
  • 5:40
    They're kind of magic and not in the Jedi way.
  • 5:45
    I mean, we choose them randomly and if you misspell one somewhere,
  • 5:49
    you won't get an error but things won't work right.
  • 5:53
    To make things worse, in BattleManager when you see these strings,
  • 5:56
    it's not clear what other strings might be possible.
  • 6:00
    Are there other battleTypes
  • 6:02
    were forgetting to handle
  • 6:03
    and if we wanted to add or remove a battleType,
  • 6:06
    what other files would we need to change?
  • 6:09
    It's really common to have magic strings like these,
  • 6:13
    but they can become hard to keep track of.
  • 6:15
    You end up referencing these exact little strings in many places.
  • 6:21
    Of course, object-oriented code has an answer.
  • 6:24
    It's called class constants and it works like this.
  • 6:28
    Inside any class you can use a special key word called "const"
  • 6:32
    followed by a word,
  • 6:34
    which is usually in all upper case like TYPE_NORMAL
  • 6:38
    and equals a value, normal.
  • 6:41
    Repeat this for const
  • 6:43
    TYPE_NO_JEDI = 'no_jedi'
  • 6:48
    and const TYPE _ONLY_JEDI = 'only_jedi';
  • 6:56
    Constants are like variables except they can never be changed.
  • 7:01
    You can call the constants anything.
  • 7:03
    By adding TYPE_ before each one, it helps me remember
  • 7:07
    what these were used for, battleTypes.
  • 7:10
    You can also add these to any class.
  • 7:14
    I chose BattleManager because these types are used here.
  • 7:17
    As soon as you do this, you can replace the random string
  • 7:20
    with BattleManager::TYPE_NO_JEDI.
  • 7:33
    Below that use, BattleManager::TYPE_ONLY_JEDI.
  • 7:39
    That will work the exact same way as before.
  • 7:43
    In index.php, do the same thing.
  • 7:46
    php echo BattleManager::TYPE_NORMAL.
  • 7:53
    Copy that and replace it with
  • 7:57
    TYPE_NO_JEDI and TYPE_ONLY_JEDI.
  • 8:03
    To prove it still works, refresh this page.
  • 8:07
    Everything's still happy.
  • 8:08
    In a sense, nothing changed.
  • 8:11
    But now these magic strings have a single home,
  • 8:14
    at the top of BattleManager.
  • 8:17
    If we ever needed to change these strings,
  • 8:19
    we can do it in just 1 place.
  • 8:22
    This also gives these strings some context.
  • 8:26
    These are obviously related to BattleManager
  • 8:28
    and we can probably look here to see how they're used.
  • 8:32
    We can also document what they mean by adding some details above each type.
  • 8:39
    Now check this out, when some other developer looks inside
  • 8:43
    index.php, instead of seeing some magic,
  • 8:46
    meaningless strings like before,
  • 8:49
    they'll see these constants and think, Oh!
  • 8:52
    BattleManager::TYPE_NORMAL.
  • 8:56
    Let me go look in that class to see what this means.
  • 9:01
    Oh, hey, there's even some documentation.
  • 9:04
    So anytime you have a string or other value
  • 9:06
    that has some special meaning but will never change,
  • 9:10
    make it a constant and stay happy.
Loading ...

The Wonder of Class Constants

Loading...

In this tutorial, you'll get the project files up and running and learn all about class constants in object-oriented PHP.

If you're serious about getting really good at this stuff, code along with me. To do that, download the source code from this page, unzip it, and move into the start directory. When you do that, you'll have the same code that I have here. Open up the README file and follow the instructions inside to get things setup.

When that's done, open your favorite terminal application, move into the directory, and — like we've done in the previous courses — start the built-in php web server by running:

php -S localhost:8000

This is a great server to use for development. Then, in our browser, we can go to http://localhost:8000. Here is our beautiful Battles app!

Downloads: 
Log in or sign up to download companion files.
Additional resources: