PHP Type-hinting and Semantic Methods

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:04
    I need to show you something,
  • 0:06
    so start another battle between some Jedi Starfighters.
  • 0:16
    It works!
  • 0:18
    But if I refresh enough times,
  • 0:22
    come on,
  • 0:24
    yes, it blows up.
  • 0:26
    "Argument 2 passed to BattleResult::_construct() must be an instance of Ship, null given."
  • 0:35
    In BattleResult, because we're good programmers,
  • 0:38
    we type-hinted the two ship arguments.
  • 0:40
    But if you look at the battle function,
Loading ...

PHP Type-hinting and Semantic Methods


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 tutorial we'll fix that problem by creating a isThereAWinner semantic method in our BattleResult class.

Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.