Buy

This object-oriented, or OO, stuff gets really fun once we have multiple objects. Afterall, it takes at least 2 ships to start a battle.

But first, let's summarize all of this printing stuff into a normal, traditional, flat function called printShipSummary(). It'll take in a Ship object as an argument, which I'll call $someShip. Now, copy all the echo stuff into the function.

The argument to the function could be called anything. Since I chose to call it $someShip, all of the $myShip variables below need to be updated to $someShip also. This is just classic behavior of functions - nothing special. I'll use a trick in my editor:

62 lines play.php
... lines 1 - 44
function printShipSummary($someShip)
{
echo 'Ship Name: '.$someShip->getName();
echo '<hr/>';
$someShip->sayHello();
echo '<hr/>';
echo $someShip->getNameAndSpecs(false);
echo '<hr/>';
echo $someShip->getNameAndSpecs(true);
}
... lines 55 - 62

Ok, saving time!

Back at the bottom of the file, call this like any traditional function, and pass it the $myShip variable, which we know is a Ship object:

62 lines play.php
... lines 1 - 44
function printShipSummary($someShip)
... lines 46 - 53
}
... lines 55 - 56
$myShip = new Ship();
$myShip->name = 'TIE Fighter';
$myShip->weaponPower = 10;
printShipSummary($myShip);

So we're throwing around some objects, but this is just normal, flat, procedural programming. When we refresh it's exactly the same.

Create Another Ship

Now, to the good stuff! Let's create a second Ship object. The first is called Jedi Starship and has 10 weaponPower. Let's create $otherShip. And just like if 2 ships landed in your dock, one will have one name, and another will be named something different. Let's call this one: Imperial Shuttle. Set its weaponPower to 5 and a strength of 50:

73 lines play.php
... lines 1 - 59
$myShip = new Ship();
... lines 61 - 63
printShipSummary($myShip);
... line 65
$otherShip = new Ship();
$otherShip->name = 'Imperial Shuttle';
$otherShip->weaponPower = 5;
$otherShip->strength = 50;
... lines 70 - 73

These two separate objects both have data inside of them, but they function indepedently. The only thing they share is that they're both Ship objects, which means that they both share the same rules: that they have these 4 properties and these 3 methods. But the property values will be totally different between the two.

This means that we can print the second Ship's summary and see its specs:

73 lines play.php
... lines 1 - 65
$otherShip = new Ship();
$otherShip->name = 'Imperial Shuttle';
$otherShip->weaponPower = 5;
$otherShip->strength = 50;
echo '<hr/>';
printShipSummary($otherShip);

Now we get a print-out of two independent Ships where each has different data.

Leave a comment!