The Object-oriented PHP Project

Video loading...

  • 0:11
    The Object-oriented PHP Project
  • 0:13
    with Leanna Pelham
  • 0:14
    I am so glad you're here today.
  • 0:16
    I'm not kidding.
  • 0:17
    Because we're intro into one of my absolute favorite topics,
  • 0:23
    object-oriented programming.
  • 0:25
    This is what gets me up in the morning excited to go to work.
  • 0:29
    This is why I love to code.
  • 0:31
    And I hope you'll be as geeked about these new tools as I am.
  • 0:34
    Like always, we're going to learn this stuff by building a real app.
  • 0:39
    Let's take the app for a test drive.
  • 0:41
    You can, of course, use a web server like Apache
  • 0:43
    and set up a virtual host, but I prefer the built-in PHP web server.
  • 0:49
    Open up a terminal and move into the oo directory.
  • 0:53
    From here, to start with the built-in web server,
  • 0:55
    you can call PHP with the -S option and pass an address.
  • 1:03
    It'll hang there, and that means it's working.
  • 1:06
    We instantly have a web server at the address
  • 1:09
    that's serving from our directory.
  • 1:11
    Let's go to the browser and try it out.
  • 1:15
  • 1:16
    Welcome, to OO Battleships of Space.
  • 1:20
    This awesome app does one important thing.
  • 1:23
    It lets you fight one ship against another.
  • 1:26
    We have four ships.
  • 1:27
    Each has a weapon power, which is your offense;
  • 1:30
    strength, which is your defense; and Jedi factor.
  • 1:33
    This last one randomly causes one ship
  • 1:36
    to go all Luke Skywalker on another and destroy it instantly.
  • 1:41
    OK, let's put four Jedi Starfighter against one
  • 1:45
    giant Super Star Destroyer.
  • 1:47
    A Super Star Destroyer is a lot more powerful, so it'll probably win.
  • 1:52
    Oh, stunning upset!
  • 1:54
    The Jedi Starfighters won.
  • 1:56
    Probably they used their Jedi ways to find some crazy weakness.
  • 2:01
    Of course, we can go back and do a rematch.
  • 2:03
    Four Jedi Starfighters against another Super Star Destroyer.
  • 2:10
    And now, the destroyer wins.
  • 2:12
  • 2:13
    So much for the good guys.
  • 2:16
    Behind this, we have exactly three PHP files.
  • 2:20
    First is index.php, which is the home page.
  • 2:24
    It requires functions.php and calls getShips from it.
  • 2:29
    All that does is create this nice associative array of four ships.
  • 2:33
    Each has a name, weapon power, Jedi factor, and strength keys.
  • 2:39
    Back in index.php, use those below in a foreach to create a table.
  • 2:46
    And we use it again to create the options and the select dropdowns.
  • 2:51
    When we submit, it POSTs to battle.php.
  • 2:56
    That also calls getShips, reads some POST data to figure out which
  • 3:01
    ships are fighting, and how many.
  • 3:03
    And eventually, calls a battle function that finds the winner.
  • 3:06
    I'll show you that later.
  • 3:09
    Then we use that outcome to show a status report below.
  • 3:13
    There's our app.
  • 3:14
    It's got no object-oriented code yet.
  • 3:17
    And you know what?
  • 3:18
    That makes me sad.
  • 3:20
    Time to fix it.

The Object-Oriented PHP Project


This series introduces important concepts in object-oriented PHP. It is authored and produced by our partners at KnpUniversity. In this series, you'll be building a PHP app using PHP and refactoring the code, step-by-step, using concepts in OO-PHP such as classes, methods, access control, type hinting, and constructors. You'll learn how to have one object interact with another and by the end of this project, your PHP app will be sporting some shiny new object-oriented PHP.

In this lesson, Leanna introduces you to the project and shows you how to get it up and running. So, look for the project zip file under the Resources heading below and we'll walk you through the process of getting the app up and running on your computer using the built-in PHP server. As long as you have PHP installed on your computer and a code or text editor, you should be able to complete the lessons in this series. (A full stack web server (i.e. Apache/MySQL/PHP) is not required, only PHP.) Follow along by running commands from the start directory.

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