Yogesh Chauhan's Blog

Understand Inheritance in PHP in this Basic Example (For Beginners)

in PHP on February 15, 2020

In This Following Article I Have Demonstrated How To Create Class And Its Objects.

OOP, Class And Objects Strategies For Beginners (PHP)

Let's get some basic understanding of inheritance first.

In object-oriented programming, inheritance is the mechanism of basing or creating an object or class upon another object or class, to give that object or class similar implementation. 

If we use base as another object then it would be called prototype-based inheritance and if we use class then it would be called class-based inheritance.

The new classes are called subclasses or child classes or child objects.

Once we know how to write class and objects we can easily derive subclasses from it. This can save lots of time in writing code again and again. For example, if we have a User class and we want to use it to create same class Student but with some more properties in addition to user class. Imagine writing down all those lines of code for each and every time you want a new Student user. That's when this inheritance concept is very helpful. 

We can do that by using extends operator. When you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

Let's take a look at the example:


//code inside PHP

$object = new Student;
$object->name = "Jon";
$object->password = "rt456ys8^&%(*&%";
$object->phone = "111 111 1111";
$object->email = "[email protected]";
$object->class = "web development";
$object->semester = "second";
$object->display();

class User {
  public $name, $password;
  function setup_student_profile()
  {
    echo "code goes here";
  }
}

class Student extends User {
  public $phone, $email, $class, $semester;
  function display() {
    echo "Student Name: " . $this->name . "<br>";
    echo "Password: " . $this->password . "<br>";
    echo "Phone Number: " . $this->phone . "<br>";
    echo "Email Address: " . $this->email;
    echo "Email Address: " . $this->email. "<br>";
    echo "Class: " . $this->class. "<br>";
    echo "Semester: " . $this->semester;
  }
}

In the example above, the original or base class, User, has two properties. $name and $password. Also, there is a method to create student profile into database. I have not mentioned full code in the method but that's not the focus of this article.

The Student class extends those two properties of User class as it's a subclass of User class. Also, the Student class has it's own 4 properties. $phone, $email, $class, $semester. Student class has a method of displaying the properties of the current object. It uses $this variable, which refers to the current values of the objects being accesses. 

The out of the code above will be:


//output

Student Name: Jon
Password: rt456ys8^&%(*&%
Phone Number: 111 111 1111
Email Address: [email protected]
Class: web development
Semester: second

So, as you can see the subclass Student has successfully derived all the properties of it's parent class, User.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What’s new in WordPress 5.5?WordPressHow to make WordPress main stylesheet (style.css)?WordPressHow to create a CSS full page background image?CSSComposing and Extracting Components in ReactReactHow to select an element using its ID without the high specificity of the ID selector?CSSWhat’s a Strict mode in JavaScript?JavaScript