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.

Do you want high-paying freelance clients?

I’ll show you what I learned over the last 15 years about building a 100K freelance business from complete scratch. What I did to land projects with Inc. Magazine, Tim Ferris, Lewis Howes, Michael Hyatt and others. Just enter your email address in the box below and let’s get started:

Leave a Reply on Twitter

Due to excessive spam, I’ve disabled comments on my website. If you have a question or would like to leave a comment, don’t hesitate to contact me on Twitter.

You might also like

Hack Culture Needs to Die

Anybody else tired of “hack” culture? “7 Hacks to Triple Your…” “A Secret Hack to Instantly Double Your…” “52 Little-Known Hacks to…” I asked my

Read More »
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

The simple secret to high-paying freelance clients?

Enter your email below and I’ll show how to charge more and build a 100K freelance business from scratch:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Jason Rumley

Jason Rumley

John has a habit of over-delivering on the expectations he sets up. That’s why he’s the best.

Aaron Gott

Aaron Gott

John has a particular knack for the development and training of others.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Oliver Wainwright

Oliver Wainwright

I’m a fan. I have completed several of John’s courses. I find him very knowledgeable and he has a great delivery.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Steve Dimmick

Steve Dimmick

John has provided expert knowledge and advice on multiple occasions that have helped me better serve my clients. John is a Rockstar!

Michael Skye

Michael Skye

John is a man of integrity, who gives generously of himself to projects and people he cares about.