Close

December 21, 2017

Inheritance In a PHP Class and Object-Oriented Programming

Let’s talk about inheritance in PHP. First, here’s how you do it:

<?php
class Daddy {
    public function my_method() {
        echo "I'm the parent";
    }
}

class Son extends Daddy {
    
}

$child = new Son;
$child->my_method(); // This outputs "I'm the parent" from the parent class

The child is the class DOING the extending. The parent is the class BEING extended. And, the child inherits any public or protected properties and methods from the parent. This is why “Son” has no methods, but still outputs the results of my_method(). Because it inherited that method from Daddy.

But, private methods and properties are NOT inherited. So, this won’t work:

<?php
class Daddy {
    public function my_method() {
        echo "I'm the parent";
    }
    
    private function you_cant_haz() {
        echo "Only Daddy gets me!";
    }
}

class Son extends Daddy {
    
}

$child = new Son;
$child->you_cant_haz(); // This triggers a fatal error

You can also override inherited methods in the child. Like this:

<?php
class Daddy {
    public $var = "Call me Daddy! ";
    
    public function my_method() {
        echo $this->var;
    }
    
    private function you_cant_haz() {
        echo "Only Daddy gets me!";
    }
}

class Son extends Daddy {
    public $var = "Uh, that's creepy. No!";
    
    public function my_method() {
        echo $this->var;
    }
}

$parent = new Daddy;
$parent->my_method(); // This outputs "Call me Daddy!"

$child = new Son;
$child->my_method(); // This outputs "Uh, that's creepy. No!"

One example use case for this is WordPress widgets. When creating a new widget, your individual widget class extends WP_Widget to access and override its default methods. WordPress then does the “magic” of putting your widget together simply by you overriding the WP_Widget methods in your class.

Another example is the example OOP application shell I show you how to build in my object-oriented programming course. I give you a primer on MVC, which is obviously super popular right now, but is only one among many design patterns. But, I also show you another pattern I learned a few years back that I personally like better… and it involves some strategic inheritance like this. You can learn more about and enroll in my object-oriented programming course here.

6 Comments on “Inheritance In a PHP Class and Object-Oriented Programming

Douglas Heller
December 21, 2017 at 3:11 pm

Hey John
Thanks for this. This is about the point in the whole “class” discussion where my mind starts to get boggled a bit. I’m not the sharpest canyon in the programming box. It’ll take a little digesting but the more I look & mull it over the better my chance of coming to some kind of actual understanding.

Reply
John Morris
December 21, 2017 at 9:34 pm

Yeah, it takes just digging into it. If you think of OOP as primarily an approach to application-building, I think it helps. It’s mostly about applications structure more than it is a right or wrong way to code.

Reply
Samsor ithnin
December 22, 2017 at 1:08 am

Hai john,where do you get this blog template.its simple and clean.can you tell me where do you get this.About your php 101 course its amazing. You are really really good at explaining something.i will buy oop course soon.

Reply
John Morris
December 22, 2017 at 8:08 am

Appreciate it! The theme I’m using is LayersWP.

Reply
Samsor ithnin
December 22, 2017 at 1:13 am

And also can you explain about php folder structure for building application.its makes me struggle.i know you know the best.

Thanks john.

Reply
John Morris
December 22, 2017 at 8:10 am

Well so, folder structure is really determined by application structure, imo. So, depending on the design pattern you’re using or just how you choose to structure your application, that will heavily influence how you want to structure your folders. If you truly are going to get my OOP course soon, I talk about this in there in the autoload lesson. That’s where it really starts to become obvious.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.