আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-6: PHP OOP Inheritance

What is Inheritance

In Object Oriented Programming, the process of sharing some methods and properties from one class to another class is called inheritance, Or, Inheritance is a process, in which, any class or object can attains the features of any other class or object. It’s easier to say that, if a child class inherits some of the features of it’s parent class and then use it, this process is called inheritance.

How to create child class in PHP?

To create a child class, you have to name the child class after the class key word, then we have to write extends key word before the name of the parent class. Such as:

<?php
class ParentClass {
  // properties and methods here
}
 
class ChildClass extends ParentClass {
  // additional properties and methods here
}
?>

How does Inheritance works in PHP?

Basically the Inheritance affair comes based on the ideas of Parent Class and Child Class. When you create a child class, it will inherit all of its parent class’s protected and public Property, Method, Constant. More simply, When a class extends to another class, we can access all of Parent Class’s Protected and Public Property, Method and constant without define them to Child Class. As well as, we can include more new property and method in the child class. Let’s get a better understanding with a example of a Web Forum Application:

<?php
class Member {
  public $username = "";
  private $loggedIn = false;
  public function login() {
    $this->loggedIn = true;
  }
  public function logout() {
    $this->loggedIn = false;
  }
  public function isLoggedIn() {
    return $this->loggedIn;
  }
}
?>

Explanation: The upper member class is a class for a web forum application. Where for Forum members login(), logout(), isLoggedIn() method has been created. Since the Forum Administrators are Members too, Now you can create a new child class from the Member Class, Which will be named as Administrator Class. And the advantages are, all the protected and public property of member class, Method and constant will be inherited in Administrator class. Let’s create an Administrator Class from the member class as a child class:

<?php
class Administrator extends Member {
  public function createForum( $forumName ) {
    echo "$this->username created a new forum: $forumName<br>";
  }
  public function banMember( $member ) {
    echo "$this->username banned the member: $member->username<br>";
  }
}
?>

Explanation: Notice that we do not have to do a lot of work in the Administrator class. Administrator Class can login, logout and can check is the user logged In with its Member Parent Class as it has inherited these methods from it’s Member Parent Class. Now you can add administrator specific methods such as createForum(), deleteForum() and banMember() to your administrator class. And the advantage is that you are getting the benefit of General Member Class and the administrator class at the same time. And that is the inheritance. Let’s look at the examples of parent and child class together:

<?php
class Member {
    public $username = "";
    private $loggedIn = false;
    public function login() {
        $this->loggedIn = true;
    }
    public function logout() {
        $this->loggedIn = false;
    }
    public function isLoggedIn() {
        return $this->loggedIn;
    }
}
 
class Administrator extends Member {
    public function createForum( $forumName ) {
        echo "$this->username created a new forum: $forumName<br>";
    }
    public function banMember( $member ) {
        echo "$this->username banned the member: $member->username<br>";
    }
}
 
// Create a new member and log them in
$member = new Member();
$member->username = "Masud Alam";
$member->login();
echo $member->username . " is " . ( $member->isLoggedIn() ? "logged in" : "logged out" ) . "<br>";
 
// Create a new administrator and log them in
$admin = new Administrator();
$admin->username = "Farhan";
$admin->login();
echo $admin->username . " is " . ( $member->isLoggedIn() ? "logged in" : "logged out" ) . "<br>";
// Displays "Mary created a new forum: W3programmers"
$admin->createForum( "W3programmers" );
// Displays "Farhan banned the member: Masud Alam"
$admin->banMember( $member );
?>

Explanation: Notice that none of the properties and methods used by the Administrator Object handler in line no. 33,34 and 35 are the Administrator Class property and Method. Everything is inherited from Member Class. And this is why they were able to use them with the object of the Administrator class.

What is Method Overriding?

Generally when a child class is created, Then it inherits all of its parent class’s Protected Property and Methods. But sometimes we need to re-write any method of parent class again in the child class by using the same name. It is usually only necessary when Parent Class’s method can not fulfill all the purposes of the child class. That’s mean, we need to add some new features. It is called Method Overriding. If any method of Parent Class repeats itself with the same name and the same parameter in the Child Class, then it’s called Method Overriding in Object Oriented Programming. See the example below:

class ParentClass {
  public function myMethod($x) {
    // (method code here)
  }
}
class ChildClass extends ParentClass {
  public function myMethod($x) {
    // For ChildClass objects, this method is called
    // instead of the parent class's MyMethod()
  }
}

Now let’s give a practical example: Let’s assume that, Our Member class’s login method works just for login. Now if the Administrator Class wants, it is necessary to keep a log entry when anyone logged in. And for this, we’ll use the Method Overriding feature. Let’s see:

<?php
class Member {
  public $username = "";
  private $loggedIn = false;
  public function login() {
    $this->loggedIn = true;
  }
  public function logout() {
    $this->loggedIn = false;
  }
}
class Administrator extends Member {
  public function login() {
    $this->loggedIn = true;
    echo "Log entry: $this->username logged in<br>";
  }
}
// Create a new member and log them in
$member = new Member();
$member->username = "Masud Alam";
$member->login();
$member->logout();
// Create a new administrator and log them in
$admin = new Administrator();
$admin->username = "Farhan";
$admin->login();           // Displays "Log entry: Farhan logged in"
$admin->logout();

What is the procedure for using Parent Class’s Method in a Child Class’s Method?

If you want to use the Parent Class method in the child class, we have to use the Parent keyword and scope resolution operator (::). Just like this:

parent::parentMethod();

Let’s give a practical example: Notice that, In the above example of Method Overriding, we have written all the codes of login method of Parent Class in Child Class’s login method. But if we just used the parent keyword, then we didn’t have to rewrite all the login method code of Parent Class. See the example below:

<?php
class Member {
  public $username = "";
  private $loggedIn = false;
  public function login() {
    $this->loggedIn = true;
  }
  public function logout() {
    $this->loggedIn = false;
  }
}
class Administrator extends Member {
  public function login() {
    parent::login(); // Using parent keyword
    echo "Log entry: $this->username logged in<br>";
  }
}
// Create a new member and log them in
$member = new Member();
$member->username = "Masud Alam";
$member->login();
$member->logout();
// Create a new administrator and log them in
$admin = new Administrator();
$admin->username = "Farhan";
$admin->login();           // Displays "Log entry: Farhan logged in"
$admin->logout();

Explanation: Notice that we have used parent keyword instead of copying the original code of parent class in line number 14.

What is the way to prevent inheritance?

Closing or Preventing Class Inheritance means that stopping creating a child class from parent class and closing method Overriding. Often, due to Inheritance and Method Overriding, class security may face some problem. Or the program may contain complex shapes. And for Class and Method security we have to prevent creation of Child Class and Method Overriding, and for that we have to use final keyword before the class and method name. Let’s see How to stop Method Overriding with the Final keyword:

<?php
class Member {
  public $username = "";
  private $loggedIn = false;
  public final function login() {
    $this->loggedIn = true;
  }
  public function logout() {
    $this->loggedIn = false;
  }
  public function isLoggedIn() {
    return $this->loggedIn;
  }
}
 
class NaughtyMember extends Member {
  public function login() {
    $this->loggedIn = true;
    // Do something bad
  }
}
?>

Result:

Fatal error: Cannot override final method Member::login()

Let us now see how the class Inheritance can be closed with the final keyword:

<?php
final class Member {
  // This class can't be extended at all
}
class Administrator extends Member{
// Will show Fatal Error
}
?>

Result:

Fatal error: Class Administrator may not inherit from final class (Member) in on line 7

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

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