Module Development

PHP Type-hinting and Semantic Methods for Drupal 8, 9

Sprout Video

We now have a BattleResult class, and we type-hinted the two Ship arguments. But now, if you look at the battle() function, there's a case where the ships can destroy each other. When that happens, there is no winning or losing ship—they're both null. Since null is not a Ship object, PHP gets angry and throws an error. In this PHP tutorial we'll fix that problem by creating a isThereAWinner semantic method in our BattleResult class.