04.
Optional type-hinting & Semantic Methods
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.
9 Comments
I know that this is a older course, but nowdays as i speak in PHP you can actually put null as ? (question mark):
Hey DXS,
Yes, good catch! You're correct, in modern PHP (starting from PHP 7.1), you can use nullable types if you want e.g. those properties allow null values, i.e. be nullable. So in your specific example
$this->winningShipproperty may be eithernullor an instance of aShipclass. The same is true for$this->losingShipproperty.Cheers!
Hi
My question is about the isThereAWinner method. You have access to private properties inside the class. So, could you please explain why don't you use just:
return $this->winningShip != null;instead of
return $this->getWinningShip() !== null;?
I mean, what is the advantage of using method in such cases?
Hey Serhii,
Sorry for the late replay, we had temporary outage with Disqus comments synchronization :/
About your question, are you asking about calling method vs calling property directly? Or are you asking about the difference between "!= null" and "!== null"?
Well, actually, if we're talking about simple getters that just return values - I'd recommend you to access property directly, i.e. call "$this->winningShip". First of all, maybe if you would do so - you even won't need that getWinningShip() method at all? If so, that's great. But if you still call that getWinningShip() somewhere outside the class, I'd still recommend you access properties directly in class, easier for refactoring in the future I think, but it depends. So, there's no a big difference at all.
But Abiola below also have a good point, but as I said, it depends on the case, don't think too much about it. ;)
Cheers!
Hi!
Thanks for your answer! My question was about calling method vs calling property. As I said below in my reply to Abiola, when I faced a real challenge, I understood the advantage of calling the method. And I also agree with you - it depends on situation.
PS. What about != and !== I know the difference. I should write !== and even better use Yoda conditions, I mean null !== $this->getWinningShip()
Hey Serhii75
You should use
!==every time you can, it's just safer, if you find yourself with the need of using "normal" comparison (!=), you should take a minute and find out why you need it.About Yoda conditions, well, personally, I don't like them but it's a standard in Symfony internals, so it's up to you if you want to adopt it or not :)
Cheers!
Serhii75 I see your point. I suppose the advantage is 'future-proofing'.
Such that in future if additional logic is ever needed before outputting the winningShip, the logic would definitely sit within getWinningShip() method and then we would not need to worry about adding this additional logic to places where winningShip might have been used directly.
Thanks for the explanation! And I have to say that just recently in the process of refactoring I just encountered this situation and now I understand better that the method is preferred (yes, you are right, talking about additional logic)
Hey Abiola,
Thanks for sharing your thoughts on it! Makes sense as well.
Cheers!
"Houston: no signs of life"
Start the conversation!