03.
Sharpening the Battle Result with a Class
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.
8 Comments
At 7:20
$battleResult->getWinningShip()->getName();
How is getName() connected with getWinningShip()?
getWinningShip is in the BattleResult Class and getName is in the Ship Class.
Does the Class not need to be extended orso? I am confused.
Hey @Farry7!
No worries! This stuff is hard! Let me see if I can help :).
Let's look at each part:
1) You're correct that
$battleResult/code> is an "instance" of the BattleResult class, which means that when we callgetWinningShip()we are calling thegetWinningShip())` inside the BattleResult class.2) Now, when we call
$battleResult->getWinningShip(), what value does that return? It returns aShipobject.3) So, when we call
->getName(), that is actually being called on theShipobject, not onBattleResult.That still... may not be a good explanation, so let's walk through the whole flow :).
A) The
battle()method inBattleManageris what's responsible for instantiating theBattleResultobject - https://symfonycasts.com/screencast/oo-ep2/battle-result-model#codeblock-ea7db2497c - when it does this, it passes$winningShipas the 2nd argument, which is aShipobject.B) Then, in
Ship's__construct()method, that 2nd argument (which, remember, is aShipobject) is set onto the$winningShipproperty ofBattleResult: https://symfonycasts.com/screencast/oo-ep2/battle-result-model#codeblock-ea7db2497cC) Finally, when we use the
BattleResultobject:$battleResult->getWinningShip()->getName(). To be more clear, we could write this one line onto multiple lines:Really,
$battleResult->getWinningShip()->getName()is just a longer version of what I wrote above.Let me know if that helps!
Cheers!
I am new to OOP, I was lost in chapter 1 especially with mile long Class names lol.
Hey Travel,
We're sorry about that, and thank you for your feedback! I see you're on a 3rd chapter now, so may I think that you finally nailed the 1st chapter? What exactly was confusing in 1st chapter? Could you tell us a bit more? And also, do you need any help/clarification on some things you didn't get in the 1st chapter?
Cheers!
I am learning a ton from these videos, but I am a novice to programming. To answer your question, I do follow all steps, but sometimes I move on to next chapter even though I did not get everything on the previous chapter. As I mentioned I am new to programming lol. Thank you
Hey Travel,
Ah, ok, I see. Well, it's totally OK if you don't get *everything* from the video, especially when you're a newbie ;) Sometimes next chapters may answer most of your questions, or make things more obvious for you.
But don't hesitate to ask questions in comments below videos if you didn't get something from the video - we will try to explain it to you from a different side :)
Cheers!
"And gone are the days of needing to use weird associative arrays: BattleManager::battle() returns a nice BattleResult object." Unless I missed something, you can't call `battle()` as a static function, because it doesn't have a `static` keyword.
Hey boykodev
That's just a way to reference to a method of a class, like in some sort of documentation file but that is not actual code.
Cheers!
"Houston: no signs of life"
Start the conversation!