Override an Inherited Method

Video loading...

  • 0:00
    Override an Inherited Method Leanna Pelham
  • 0:04
    Let's take out this demi code and get to the real stuff.
  • 0:10
    Our database is created via this init_db script
  • 0:14
    which you can execute from the command line whenever the mood strikes
  • 0:18
    to make sure that your database is set up correctly. Ding.
  • 0:23
    This creates a table with a team column. In here, we can see that the first two team columns
  • 0:29
    are team rebel and the second two are team empire.
  • 0:34
    Since these two ships work differently inside of our shiploader where we take that data
  • 0:39
    and turn it into ship objects, I want to create ship objects for the empire and the rebels.
  • 0:45
    So let's do that.
  • 0:49
    If $shipData['team'] == 'rebel' which is the key inside the database,
  • 1:00
    then we'll have $ship = new RebelShip($shipData['name']);
  • 1:08
    Else will throw in our normal ship line which represents the empire ship.
  • 1:13
    Okay, this doesn't have anything to do with object oriented coding.
  • 1:18
    It's just a nice example of a use case for multiple classes.
  • 1:22
    We have a database table, and you can created different objects from that table.
  • 1:25
    This is nice because we'll be able to have these two objects behave differently.
  • 1:31
    So far rebel ship and ship have all the same stuff except for the one extra method
  • 1:37
    I have on rebel ship that I'm not using. If we go back and refresh,
  • 1:42
    everything still works perfectly. I'm fairly certain that two of these are rebel ship objects
  • 1:48
    and the other two are ship objects, but we can't really tell right now.
  • 1:53
    Clearly we need to add identifiers so we know who to cheer on.
  • 1:59
    To do this, start by adding public function get type to our ship and return a description like empire.
  • 2:07
    Since we added that to ship, we can call get type on both ship and rebel ship.
  • 2:13
    Back in index.php toward the bottom add a new column for this called type.
  • 2:23
    And echo $ship->getType
  • 2:30
    Back to the browser and refresh.
  • 2:33
    Oh crap, everything has joined to fight for the empire
  • 2:36
    which makes sense. Both ship classes use the same method.
  • 2:41
    Time for the next really powerful thing with inheritance.
  • 2:46
    In addition to adding methods to a subclass like rebel ship, you can override methods.
  • 2:52
    Copy the get type from ship and paste it into rebel ship.
  • 2:56
    And change what it returns to rebel.
  • 3:00
    Rebel ship copies the entire blueprint of ship, but it can replace any of those pieces.
  • 3:07
    When we refresh now, we have two rebel ships in addition to our two empire ships.
  • 3:12
    Excellent. A key part of this is that the parent get type class is never called.
  • 3:18
    For all rebel ship objects, it is completely replaced.
  • 3:23
    If I echo parent function inside of get type in the ship class and refresh,
  • 3:28
    we see our ugly text echoing for the empire ships and not the rebel ships.
  • 3:37
    This is thanks to our parent function not being called in rebel ship.
  • 3:42
    Onto more methods. Another one on ship is is funcitonal which we set up to have a 30% chance
  • 3:50
    of a ship being broken, which is what our cute cloud here indicates.
  • 3:56
    But we all know that the rebels are really scrappy, and they don't have the luxury
  • 4:00
    of letting their ships get broken. Even if they're kind of broken, they still fly and make it work
  • 4:06
    which is just one more reason why the rebels are awesome.
  • 4:09
    So I need to set this up so the rebel ships are never showing as broken,
  • 4:14
    which we can do really easily by overriding is functional inside of rebel ship.
  • 4:20
    Let's update this to return true which will never show a rebel ship as broken.
  • 4:25
    When we refresh now, the rebel ships always have sunshine
  • 4:29
    and the empire ships sometimes have adorable clouds.
  • 4:33
    By having two classes, we're starting to shape the different behaviors and properties of each
  • 4:38
    while still keeping most things in common and not duplicated.
Loading ...

Override an Inherited Method


In this tutorial, I will show you how to override a method that you’ve inherited from another class. By having two classes we are starting to shape the different behaviors and properties of each, while still keeping most things in common and not duplicated.

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.