05.
Objects are Passed by Reference
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
5 Comments
The last code listing is wrong - it's something about database init, while it should be (as I thing) a fragment of battle.php
Yes, you're totally right! It was fixed and published already.
Thank you, Nailee!
I noticed that if you run this with the following parameters:
~5 jedi ships against 5 jedi ships (or any match up that both sides are equal) ~
whenever one of them is using the jedi power, the health of the other does not seem to get set to zero.... see below:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Matchup:
1 CloakShape Fighter VS. 1 CloakShape Fighter
Winner:
CloakShape Fighter
The CloakShape Fighter used its Jedi Powers for a stunning victory!
Ship Health
CloakShape Fighter 46 CloakShape Fighter 46
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
one of them should have ended with a zero health.
I am new to PHP and cannot seem to find why this is happening.
Hi there!
Ah, this is a bug with our app! Nice find! But, it's an awesome bug - because it's super relevant to this chapter - about "references". Here's what's happening:
1) In battle.php, we get an array of ships by saying $ships = $shipLoader->getShips(). This is an array of Ship objects
2) In battle.php, we use the ship name to get each Ship object from the $ships array. These are the lines:
And that is actually the bug! When we "fetch" the Ship object from the array, that object is passed to us "by reference" - it doesn't make a new copy of the Ship object. In other words, if $ship1Name and $ship2Name are the same, then $ship1 and $ship2 literally point to the exact same one Ship object in memory. When we pass these two variables in BattleManager(), weird things happen - because we're calling setStrength() on both ships, which is really changing the same one Ship objects two times. In essence, since $ship2->setStrength($ship2Health) is called last, whatever $ship2Health is set to will be the final value for both ships (since there is really only one Ship). This is why sometimes you'll see the final healths as both 46, but other times as both 0. But in all cases, they're the same.
The fix for this - which we may do later, as it is a bit more realistic - would be to have ShipLoader return us unique Ship objects - e.g. we add a method called $shipLoader->findShipByName($name), and it always returns a unique Ship object. This is actually more realistic: if we're having 2 ships fight each other, then they should always be different Ship objects. In the app right now, if you select the same type, we're kind of having one ship battle itself :).
GREAT find and question!
Good illustration of *pure* objects advantage :)
"Houston: no signs of life"
Start the conversation!