Friday , 17 November 2017
Home / Programming / PHP / Extending Classes – PHP OOP Tutorial
Extending Classes
Extending Classes

Extending Classes – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

Let’s talk about extending classes for a moment. One of the benefits of using classes is that it’s easy to share your classes with other developers. They can extend the class if you allow them to.  So instead of going into your class and updating the code, they can create a different class that only changes a few things here and there without ruining the structure of your code.

About Extending Classes

Let’s learn how to do this. In your classes folder, create a new file called CanadianPet.class.php. Let’s say that Canada has a new law that states that your pet’s name can not contain the letter a.  In your Canadian pet class, add this bit of code.

As you can see, we’re using the keyword extends to extend the Pet class. This is what we call a child class. A child class is basically a class that inherits all properties and methods of the class it’s extending. Since we extend the Pet class, our CanadianPet class has access to use the setPetName() and getPetName() methods without even defining the functions inside the class. It also has access to the $petName property.

We can update these properties and methods any way we like. Let’s change the way the method setPetName() works. As stated earlier, we do not want the letter ‘a’ appearing in the pet’s name. So, add this bit of code inside our CanadianPet class.

By extending classes, you can manipulate this class any way you want. As you can see, we’re using the str_replace() function to get rid of the letter ‘a’ in our pet’s name. We set this value to the petName. As you can see, we don’t even have to define the property petName as we already have access to it.

Let’s test this to see if this works. Let’s go to our index.php file and update our code to this.

Notice that we’re including both classes. In order for a class to extend another class, you must include the parent class and then the child class.

Next, instead of creating an instance of the Pet class, we create an instance of the CanadianPet class. We then use the class normally like any other class. If we were to check the output, you’ll see that we get Jck instead of Jack.

Conclusion

By extending classes, we give other developers the opportunity to manipulate our class any way we want without harming the structure of the original class. It also makes code less cluttered and easier to manage. To learn more about extending classes, then go here.

http://php.net/manual/en/language.oop5.basic.php

About Jasko Koyn

Check Also

Abstract Classes

Abstract Classes – PHP OOP Tutorial

Today, we’ll be discussing what abstract classes are and how we could use them. There …

4 comments

  1. nic oop tutorials
  2. cannot get include statement to open stream, no such file or directory. even tho those are saved in the classes folder and file names. please help
  3. I apriceate your project page I,m newbie, I allso understand it well, bravo php master.
  4. nice tutorial.. Can i get as pdf??

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link