You may heard about Interface in Object Oriented PHP Programming. But have you ever asked yourself that, what exactly interface? How it work? Or what is the functionality? As a php developer, why you have to bother about it?

DOWNLOAD SOURCE FILES FROM GITHUB

Well, if you have that kinds of questions in you mind, you may be lucky that this tutorial is for you. This writing is totally dedicated to understand Interface in OO PHP. What exactly interface do? Lets see…

 

What is Interface?

An interface is a contract between unrelated objects to perform a common function. Literally it guarantor for users to ensure that it is capable to perform all the functions of object that is defined specification. For example, cricket bat and football are totally different things in terms of shape and size, but in object representing those items in a sporting goods store inventory must be capable of interacting with that system.

It is very important to understand that, by declaring an interface and then implementing it in objects, can be hand completely different classed to common functions. The following file will represent that-

Create a file called interface.Openable.php

 

Naming convention of interface class is as like as class file. For example, class.class-name.php, now we are going to use, interface.interface-name.php. The declaring interface is also as like as class. Instead of class word, we used interface word before file name, that is required syntax. It is also important that, it does not have member variables, and it does not specify an implementation of any of its member functions. Because no implantation is specified, declaring these functions to be abstract. Telling to PHP by doing that, any class implementing this interface is responsible for providing an implementation of this functions. But if you are failed to implement all the abstract methods of an interface, PHP will raise a run-time error. You must to provide implementation of all the abstract methods.

 

How Interfaces work?

Going back to openable interface class. This interface contract with others parts of the application that says any class implementation of this interface will provide two methods, called open() and close() that we declared in interface.openable.php file where that taken no parameter. Let’s try to implement openable interface in a class.

Create the following file, called class.Window.php

Now, I am going to create another class to implement openable interface from that. Create a file called, class.Jar.php

Well, we have successfully finished implemented our interface. Now, we have to check out class and interface whether it is working or not. Create a new file called checkOpenable.php.

Now run checkOpenable.php file and sure enough that you will see output.

Let’s see how it works? Because both Window and Jar class implement that Openable interface, that you can pass both to openSomething() function. This function accepts only something that implements that openable interface, you know that you can call the functions open() and close() within it. However, you are not eligible to access the $content property of the Jar Class or utilize the lock() or unlock() methods from Window class though these are not the part of interface. The interface contract guarantees that you have open() and close(), nothing else.

 

Conclusion

Well done, you have successfully integrated interface in your class and created object based on that. By using interfaces in your application, you can allow completely different and unrelated objects to talk to each other with a guarantee that they will be able to interact on the terms specified in the interface. In nutshell, interface is a contract to provide certain methods.

 


What is your opinion about this tutorial? Do you think it is helpful for your learning? Let me know your comments or suggestion to improve in our next tutorial. Thanks for reading. Happy coding. :)

3674 Total Views 22 Views Today

tisuchi

Web Programmer, Blogger, Researcher.
Love to work on web based technology especially PHP and jQuery. Originated from Bangladesh. Currently living in Kuala Lampur, Malaysia and doing masters on Information and Computer Engineering in International Islamic University Malaysia.
suchi [at] tisuchi.com

Join in our newsletter group and get our tutorial FREE!


Enter your email address for regular email subscription