Buy

Welcome KnpU Peeps! I am so glad you're here today - I am not kidding - because we're intro'ing into one of my absolute favorite topics: object-oriented programming. This is what gets me up in the morning, excited to go to work, this is why I love to code. And I hope, you'll be as geeked about these new tools as I am.

Like always, we're going to learn this stuff by building a real app! Don't be lazy - code along with me to really get the feel for this stuff. Go to the screencast page and click to download the code. Unzip that file - you'll see a "start" directory. I'll rename this to oo and move it into a Sites/ directory in my Home folder.

There's not much going on yet guys - just 3 PHP files and some CSS and JS files. That's it. Let's take the app for a test drive!

You can of course use a web server like Apache and setup a Virtual Host, but I perfer the built-in PHP web server. Open up a terminal. Move into the oo directory. From here, to start the built-in web server you can call php with the -S option and pass it an address:

cd ~/Sites/oo
php -S localhost:8000

It'll hang there, and that means it's working: we instantly have a web server at the address that's serving from our directory. Let's go to the browser and try it out:

http://localhost:8000

VoilĂ ! Welcome to OO Battleships of Space! This awesome app does one important thing: it lets you fight one ship against another. We have 4 ships, each has a "weapon power", which is your offense, "strength", which is your defense and "Jedi Factor". This last one randomly causes one ship to go all "Luke-Skywalker" on another and and destroy it instantly.

Ok, let's put 4 "Jedi Starfighters" against 1 giant "Super Star Destroyer". A Super Star Destroyer is a lot more powerful, so it'll probably win.

Stunning upset! The Jedi Starfighters won! Probably they used their Jedi ways to find some crazy weakness. Of course, we can go back and do a re-match: 4 Jedi Starfighters against another Super Star Destroyer, and now the Destroyer wins.

How the App Works

Behind this, we have exactly 3 PHP files. First is index.php, which is the homepage. It requires functions.php and calls get_ships() from it:

106 lines index.php
<?php
require __DIR__.'/functions.php';
$ships = get_ships();
... lines 5 - 106

All that does is create this nice associative array of 4 ships. Each has name, weapon_power, jedi_factor and strength keys:

92 lines functions.php
... lines 1 - 2
function get_ships()
{
return array(
'starfighter' => array(
'name' => 'Jedi Starfighter',
'weapon_power' => 5,
'jedi_factor' => 15,
'strength' => 30,
),
'cloakshape_fighter' => array(
'name' => 'CloakShape Fighter',
'weapon_power' => 2,
'jedi_factor' => 2,
'strength' => 70,
),
'super_star_destroyer' => array(
'name' => 'Super Star Destroyer',
'weapon_power' => 70,
'jedi_factor' => 0,
'strength' => 500,
),
'rz1_a_wing_interceptor' => array(
'name' => 'RZ-1 A-wing interceptor',
'weapon_power' => 4,
'jedi_factor' => 4,
'strength' => 50,
),
);
}
... lines 32 - 92

Back in index.php, we use those below in a foreach to create a table:

106 lines index.php
... lines 1 - 3
$ships = get_ships();
... lines 5 - 65
<?php foreach ($ships as $ship): ?>
<tr>
<td><?php echo $ship['name']; ?></td>
<td><?php echo $ship['weapon_power']; ?></td>
<td><?php echo $ship['jedi_factor']; ?></td>
<td><?php echo $ship['strength']; ?></td>
</tr>
<?php endforeach; ?>
... lines 74 - 106

And we use it again to create the options in the select drop-downs:

106 lines index.php
... lines 1 - 78
<form method="POST" action="/battle.php">
... lines 80 - 81
<select class="center-block form-control btn drp-dwn-width btn-default btn-lg dropdown-toggle" name="ship1_name">
<option value="">Choose a Ship</option>
<?php foreach ($ships as $key => $ship): ?>
<option value="<?php echo $key; ?>"><?php echo $ship['name']; ?></option>
<?php endforeach; ?>
</select>
... lines 88 - 99
</form>
... lines 101 - 106

When we submit, it POST's to battle.php. That also calls get_ships(), reads some $_POST data to figure out which ships are fighting and how many, and eventually calls a battle() function that finds the winnner. I'll show you that later:

97 lines battle.php
... lines 1 - 3
$ships = get_ships();
... line 5
$ship1Name = isset($_POST['ship1_name']) ? $_POST['ship1_name'] : null;
$ship1Quantity = isset($_POST['ship1_quantity']) ? $_POST['ship1_quantity'] : 1;
$ship2Name = isset($_POST['ship2_name']) ? $_POST['ship2_name'] : null;
$ship2Quantity = isset($_POST['ship2_quantity']) ? $_POST['ship2_quantity'] : 1;
... lines 10 - 25
$ship1 = $ships[$ship1Name];
$ship2 = $ships[$ship2Name];
$outcome = battle($ship1, $ship1Quantity, $ship2, $ship2Quantity);
... lines 30 - 97

Then we use that $outcome to show a status report below:

97 lines battle.php
... lines 1 - 28
$outcome = battle($ship1, $ship1Quantity, $ship2, $ship2Quantity);
... lines 30 - 66
<h3 class="text-center audiowide">
Winner:
<?php if ($outcome['winning_ship']): ?>
<?php echo $outcome['winning_ship']['name']; ?>
<?php else: ?>
Nobody
<?php endif; ?>
</h3>
... lines 75 - 97

There's our app! It's got no object-oriented code yet. And you know what? That makes me sad. Time to fix it!

Leave a comment!

  • 2016-07-25 Victor Bocharsky

    Hey Hariadi,

    Some courses are free, but some of them are paid. To be able to play videos of paid courses - you should be logged in into your account on KnpUniversity and have a paid subscription (or have a purchased course, videos of which you want to play). Please, feel free to contact us if you still have any questions.

    Cheers!

  • 2016-07-25 Hariadi Wicaksana

    Why I can not play the video in all courses in knpuniversity?