Introduction to Inheritance in PHP

Video loading...

  • 0:10
    Introduction to Inheritance in PHP Leanna Pelham
  • 0:13
    Welcome back for episode three
  • 0:15
    of our object-oriented series. We're ready to get serious about inheritance
  • 0:19
    and not just from that rich uncle of yours.
  • 0:22
    I'm talking about extending classes, abstract classes,
  • 0:26
    interfaces, stuff that really makes object-oriented code nice
  • 0:30
    but doesn't always look easy at first.
  • 0:33
    Don't worry. This will all start to feel really familiar in a surprisingly small amount of time.
  • 0:41
    I'm already in the project that we've been working on through this series.
  • 0:44
    If you don't have this yet, download the code and use what's in the start directory.
  • 0:49
    In my terminal, I've also started the built-in web server with php -S localhost:8000.
  • 0:56
    Be careful to do that in the start directory of the project.
  • 1:01
    So far in our project, we have just this one lonely ship object.
  • 1:07
    We query things from the database and we load the ship, but exciting things are happening
  • 1:13
    and now we have a new problem. We want to model two different types of ships.
  • 1:18
    We have normal ships from the empire, and since those are kind of evil
  • 1:23
    we also now want rebel ships to set them straight.
  • 1:27
    In the browser you can see we have two rebel ships in here coming from the database.
  • 1:31
    I would really like rebel ships to fundamentally work differently.
  • 1:36
    For example, they break down less often and have higher jedi powers.
  • 1:39
    Let me show you what I mean.
  • 1:44
    Create a new php class called rebel ship. Easy.
  • 1:48
    Since rebel ships are exactly like boring old empire ships, let's create a new class
  • 1:54
    or blueprint that models how these work.
  • 1:57
    Head on into bootstrap.php and require the rebel ship file there.
  • 2:02
    We don't have an autoloader yet, so we still have to worry about these require statements.
  • 2:09
    Rebel ships are different than empire ones, but they do share about 99% of their attributes.
  • 2:15
    For example, they both have wings, fire power, defense power, et cetera.
  • 2:21
    My first instinct should be going to ship.php and copy all the contents
  • 2:27
    and paste them into rebel ship.php
  • 2:29
    since most of it will probably apply. But I shouldn't need to remind you
  • 2:34
    that this would be a silly amount of duplication in our code, which would make everyone sad.
  • 2:39
    This is our chance to let classes help us not be sad by using the extends keyword.
  • 2:42
    By saying class rebel ship extends ship, everything that's in the ship class
  • 2:50
    is automatically inside if rebel ship. It's as if all the properties and methods of ship
  • 2:55
    are now a part of the rebel ship blueprint.
  • 3:00
    In index.php, we can say rebel ship equals new rebel ship my new rebel ship.
  • 3:07
    And we can just add this to the ship's array.
  • 3:15
    Remember down here we iterate over the ships and call things like get name,
  • 3:20
    get weapon power, and get jedi factor which don't actually live inside of our rebel ship.
  • 3:26
    But when we refresh, it works perfectly.
  • 3:30
    Lesson number one, when you have one class that extends another,
  • 3:34
    it inherits—you'll hear that word a lot—all of the stuff inside that parent class.
  • 3:42
    So we can call methods like get name or get name and specs on rebel ship
  • 3:46
    because it inherits that from ship. Really rebel ship works just like a normal class.
  • 3:52
    If you want to you can add completely new functions. Let's do that with public function get favorite jedi.
  • 4:01
    That has an array of some cool jedis.
  • 4:06
    Then use array rand to select one of those.
  • 4:12
    Since this was all done in rebel ship, head over to index.php and call that method
  • 4:19
    var dump rebel ship arrow get favorite jedi.
  • 4:24
    And you can see with my autocomplete, it's showing me all of my public functions
  • 4:29
    on both ship and rebel ship. You can even see that the rebel ship methods
  • 4:34
    are displayed bolder and methods from the parent class are lighter.
  • 4:41
    When we refresh, we see our favorite random jedi. It works perfectly.
  • 4:45
    Extending classes is great for reusing code without the sad duplication.
Loading ...

Introduction to Inheritance in PHP

Loading...

In this series, we’ll continue to build the spaceship app that we’ve started in Introduction to Object-Oriented PHP and Object-Oriented PHP Part 2. In this tutorial, I’ll show you how to use the extends keyword in PHP so that you can use it to inherit properties and methods from another class.

Downloads: 
Log in or sign up to download companion files.
Additional resources: