PHP Service Classes

Video loading...

  • 0:10
    PHP Service Classes with Leanna Pelham
  • 0:13
    It's time to put our new object- oriented skills into practice.
  • 0:17
    We're working on the same out-of-this-world project.
  • 0:21
    It has ships, you choose them, and they engage in epic battles!
  • 0:31
    In an editor far, far away,
  • 0:33
    you'll see a simple application that runs this.
  • 0:37
    Index.php is the home page,
  • 0:39
    and battle.php does the magic
  • 0:42
    and shows the results.
  • 0:44
    Last time, we created a single class called Ship,
  • 0:47
    which describes all its properties.
  • 0:50
    It's like a container for one ship's details.
  • 0:53
    We use these to replace these big associate of arrays.
  • 0:58
    Now we deal with cute ship objects.
  • 1:01
    Having a huge list of flat functions in functions.php
  • 1:06
    is not a good recipe for staying organized.
  • 1:10
    But in just a few minutes,
  • 1:12
    we'll use some new classes to give our app a whole new level of sophistication.
  • 1:19
    We'll get rid of battle first.
  • 1:21
    Look at Ship.
  • 1:23
    This is a class that basically just holds data.
  • 1:26
    Some people call that state, but I'll say data.
  • 1:30
    And I'm talking about the values on a ship object's properties.
  • 1:35
    So a ship object holds data,
  • 1:38
    but it doesn't really do any work.
  • 1:40
    Sure, it has some methods on it,
  • 1:43
    but these just return that data after doing some small logic at best.
  • 1:49
    Reason number one for creating a class is this:
  • 1:52
    we need some organized unit to hold data,
  • 1:56
    but there's a second big reason to create a class—
  • 1:59
    because you need to do some work.
  • 2:02
    For example, in functions.php,
  • 2:05
    the battle function does work.
  • 2:07
    We give it two ships, it does some calculations,
  • 2:11
    executes logic to see how different strengths affect each other,
  • 2:15
    and ultimately returns the result of that work.
  • 2:18
    And we're all familiar with creating functions like this.
  • 2:21
    Here's the secret for OO.
  • 2:23
    Whenever you get the urge to create a flat function like battle,
  • 2:27
  • 2:31
    Instead, create a class with the method inside of it.
  • 2:34
    Let's do this!
  • 2:36
    Since this function is all about battling,
  • 2:39
    let's create a new class called BattleManager.
  • 2:42
    Be as creative as you want with naming.
  • 2:46
    I want to describe the methods in this class will do things related to battling.
  • 2:51
    Go copy and remove the flat battle function.
  • 2:59
    Paste it into BattleManager.
  • 3:04
    Put public in front of the function.
  • 3:07
    Remember, public means that code outside of this class
  • 3:11
    will be able to call this.
  • 3:13
    And yes, you don't have to add public,
  • 3:17
    functions default to public if you say nothing.
  • 3:20
    But let's keep things clear.
  • 3:22
    That's all you need to change.
  • 3:25
    Functions work the same inside or outside of a class.
  • 3:29
    They have arguments, they return stuff,
  • 3:32
    but we do need to change code where we call this function
  • 3:36
    in battle.php.
  • 3:38
    So how can we call this?
  • 3:41
    Well, when we want to call a method on Ship,
  • 3:44
    we need to have a ship object first.
  • 3:46
    The same is true here.
  • 3:48
    We need a BattleManager object first.
  • 3:51
    Start with a new variable called battleManager, and create a new BattleManager object.
  • 3:57
    And now say battleManager->battle.
  • 4:03
    Let's give this a shot.
  • 4:05
    Refresh battle.php.
  • 4:08
    Oh no! Class BattleManager not found!
  • 4:12
    Epic fail!
  • 4:14
    Not really!
  • 4:16
    At the top of functions.php, we have access to the Ship class
  • 4:21
    because we're requiring it.
  • 4:23
    Do the same for BattleManager.
  • 4:25
    There is a way where you can reference classes
  • 4:28
    like BattleManager without needing to worry about the require statements.
  • 4:32
    It's called auto loading.
  • 4:35
    It's really common, and you'll learn how to master it in a future episode.
  • 4:39
    But until then, if you have a class, require it.
  • 4:43
    Go back and refresh.
  • 4:45
    Cool! Totally working!
  • 4:49
    Now we have two reasons to create a class.
  • 4:51
    First, if you have some data
  • 4:54
    like properties that describe a ship,
  • 4:56
    creating a class for that is nice.
  • 4:59
    You'll create a Ship object whenever you have a set of that data.
  • 5:03
    In get_ships,
  • 5:06
    we create four ship objects.
  • 5:08
    These types of classes are sometimes called models
  • 5:12
    because they model something like a ship.
  • 5:14
    Second, if you need to make a function
  • 5:18
    that does some work, create a class and put a method in it
  • 5:21
    like BattleManager.
  • 5:24
    Or you may put multiple methods inside one class
  • 5:27
    as long as they're all thematically similar.
  • 5:30
    You'll create one of these objects like BattleManager
  • 5:34
    just one time before you need to call a method on it.
  • 5:37
    These are sometimes called service classes
  • 5:40
    because they perform work or a service.
  • 5:43
    Organizing your code to use service classes
  • 5:46
    can be tricky, but we'll learn all about that.
Loading ...

PHP Service Classes


In this series we're going to continue on from the Introduction to Object-Oriented PHP series. We're working on the same spaceship project: it has ships, you choose them, then they engage in epic battle!

In an editor, far far away, you'll see a simple application that runs this: index.php is the homepage and battle.php does the magic and shows the results. In the introduction series, we created a single class called Ship, which describes all its properties—it's like a container for one ship's details. In this tutorial we're going to replace our flat functions and create a BattleManager service class to provide the methods we'll need to do 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.