An Army of PHP Service Classes

Video loading...

  • 0:00
    An Army of PHP Service Classes with Leanna Pelham
  • 0:05
    Yay, we got rid of a flat function. Well, not so fast.
  • 0:11
    Inside battle, we're calling a flat function didJediDestroyShipUsingTheForce?
  • 0:17
    That's no bueno. This lives at the bottom of functions.php.
  • 0:22
    In our app, this is only called from inside battle, and since it obviously relates to battles,
  • 0:29
    let's move it into BattleManager.
  • 0:36
    Make it a private function. Why did I make it private? Well, do we need to use this function
  • 0:43
    from outside of this class? No, the only code using it is up in battle.
  • 0:49
    So this is a perfect candidate to be private.
  • 0:53
    Above in battle, update the calls to be $this->didJediDestroyShipUsingTheForce?
  • 1:00
    The force of our app is happy again. Now if someday we did want to use this function
  • 1:08
    from outside of BattleManager, then we could change it to public.
  • 1:12
    Okay, so why not just make everything public?
  • 1:15
    Isn't that more flexible? Yes, but making this private is nice. It means that if I want to change this function,
  • 1:24
    add arguments, or even change what it returns, I know that the only code that will be affected
  • 1:29
    will be right inside this class. If it's public, who knows what code I might break in my app.
  • 1:36
    Start with private. Make it public only if you need to.
  • 1:40
    The same rule goes for protected, something we'll talk about later with inheritance.
  • 1:46
    Let's make sure we didn't bust things. Refresh.
  • 1:52
    Yes. In functions.php, only the get_ships function remains. You guys know what to do.
  • 2:01
    Move it into a class. Should we move it into BattleManager? No.
  • 2:07
    It doesn't relate to battles. Instead, create a new class for this.
  • 2:13
    How about ShipLoader?
  • 2:17
    Let's work our magic. Go grab get_ships and move it into ShipLoader.
  • 2:27
    Remove the old commented code and make the function public.
  • 2:34
    Also rename it from get_ships to getShips. That's a more common naming standard for methods in a class.
  • 2:43
    Yep, that's great. Now we need to update the code that calls this function.
  • 2:50
    But first, open functions.php and require the new ShipLoader.php.
  • 2:58
    GetShips is used in battle.php and index.php. Start there. To call the method, create a $shipLoader variable
  • 3:08
    and create a new ShipLoader object.
  • 3:13
    Now just shipLoader->getShips.
  • 3:18
    Do the same thing in battle.php.
  • 3:24
    I think it's time to try it. Click to create a new battle.
  • 3:30
    Looks pretty good. Set up a new battle and engage.
  • 3:37
    Okay, battle.php works too. And all the flat functions are gone.
  • 3:44
    Object orient all the things. So if you look in functions.php, well there aren't any functions here,
  • 3:53
    just require statements and even those we'll get rid of eventually.
  • 3:58
    To celebrate, give this a more appropriate name, bootstrap.php.
  • 4:03
    Update this in battle.php
  • 4:08
    and index.php. Refresh once more. Let's keep going.
Loading ...

An Army of PHP Service Classes

Loading...

In this tutorial we are going to continue to remove the flat functions in our code. We'll refactor one of them to a private function and create a new ShipLoader service class to clean up the rest. With this refactoring in place, we'll be able to rename the functions.php file since it won't contain any functions any longer.

Downloads: 
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.