আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী 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-9: PHP Object Interfaces

What is Interfaces in Object Oriented Programming?

The concept of the interface in Object Oriented Programming is much like the kind of work of a Web Developer. When a Web Developer creates an HTML Template, he/she was initially given a PSD design. Through this, Web Developer understands what to do with HTML. That means color, menu and buttons and its inputs description will be given in the PSD design. A Web Developer is not only able to use or can not use the PSD design directly. This means that you must convert a PSD to HTML and then use it. And it is called Implementation. So here the PSD is an interface of the whole template. That is, what will be the template look like is described here, and interface is same for object oriented programming. You will have an interface where what type of work the classes will do, will be there. But it will not be said how the classes will work. And you can not use the interface directly, That’s why it must be implemented on class. The abstract class and the interface are almost the same, The only difference is that, an abstract class can have property, constant and abstract and non-abstract method, and those method’s may have public or protected visibility. But interface can only have methods and every methods of interface will be abstract method by default, and visibility of those methods must be public. Generally, A child class can only inherit from a parent class, That’s mean, a child class can not inherit from multiple parent classes at the same time. But on one class, multiple interfaces can be implemented at the same time. The methods of the interfaces must have the same parameter as the class it’s been implemented on and must be implemented on the class. That’s mean, You can not add or remove a parameter of any method in the implemented class. But you can add a parameter with the default value.

What is the procedure to declare a interface?

To declare an interface, we have to declare an interface name after the interface keyword. See the following syntax:

<?php
interface MyInterface {
  public function aMethod();
  public function anotherMethod();
}
?>

What is the procedure to implement the interface in class?

To implement an interface in a class, the implements keyword is given after the name of class, Then the names of the interface are written with commas(,). See the following syntax:

<?php
class MyClass implements MyInterface1,...,MyInterfaceN {
  public function aMethod() {
    // (code to implement the method)
  }
  public function anotherMethod() {
    // (code to implement the method)
  }
}
?>

Let’s see a real example using the interface:

For example, Let’s think about a web forum, There will be a Member class to bring and store Forum members from the database. And there will be a Topic Class to pick up and preserve the topics created by forum members. And they will be from one another, Because each of them will work differently. But the two classes do some common tasks like data collection and data saving.

Now, We will create an interface called Persistable for doing some common tasks like collecting and saving data from MySQL database with Member and Topic two objects. Which will specify the methods for doing common tasks of the database. See the example below:

interface Persistable {
  public function save();
  public function load();
  public function delete();
}

Now we will create our Member class, and we will implement the Persistable interface in it. This means that we must implement the save(), load() and delete() methods in the Member class. Besides, the class will have it’s own methods. See the example below:

<?php
class Member implements Persistable {
  private $username;
  private $location;
  private $homepage;
  public function __construct( $username, $location, $homepage ) {
    $this->username = $username;
    $this->location = $location;
    $this->homepage = $homepage;
  }
  public function getUsername() {
    return $this->username;
  }
  public function getLocation() {
    return $this->location;
  }
  public function getHomepage() {
    return $this->homepage;
  }
  public function save() {
    echo "Saving member to database<br>";
  }
  public function load() {
    echo "Loading member from database<br>";
  }
  public function delete () {
    echo "Deleting member from database<br>";
  }
}
?>

Now we will make a Topic class in the same way, and we will implement the persistable interface in this. This means that we must implement the save(), load() and delete() methods in the Topic class. Besides, the class will have it’s own methods. See the example below:

<?php
class Topic implements Persistable {
  private $subject;
  private $author;
  private $createdTime;
  public function __construct( $subject, $author ) {
    $this->subject = $subject;
    $this->author = $author;
    $this->createdTime = time();
  }
  public function showHeader() {
    $createdTimeString = date( 'l jS M Y h:i:s A', $this->createdTime );
    $authorName = $this->author->getUsername();
    echo "$this->subject (created on $createdTimeString by $authorName)<br>";
  }
  public function save() {
    echo "Saving topic to database<br>";
  }
  public function load() {
    echo "Loading topic from database<br>";
  }
  public function delete () {
    echo "Deleting topic from database<br>";
  }
}
?>

Now we will create a Member object to call getUsername() method and Topic Object to call showHeader() method. At the same time, we will call interface methods such as save(), load() and delete(). Which are implemented in two classes.

<?php
$aMember = new Member( "Farhan", "Dhaka", '<a href="http://w3programmers.com/">http://w3programmers.com/</a>' );
echo $aMember->getUsername() . " lives in " . $aMember->getLocation() ."<br>";
$aMember->save();
$aTopic = new Topic( "Futonto Golap", $aMember );
$aTopic->showHeader();
$aTopic->save();
?>

Result:

Farhan lives in Dhaka
Saving member to database
Futonto Golap (created on Wednesday 10th November 2017 02:19:14 AM by Farhan)
Saving topic to database

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 *