The Wayback Machine - https://web.archive.org/web/20110607201713/http://www.hiteshagrawal.com/php/oops-in-php5-using-interface

OOPS in PHP 5 Tutorial – Using Interface

This article will guide through the Interface Class in Object Oriented Programming. In simple words Interface is a class with no data members and contains only member functions and they lack its implementation. Any class that inherits from an interface must implement the missing member function body. Interfaces is also an abstract class because abstract class always require an implementation. In PHP 5 class may inherit only one class, but because interfaces lack an implementation any number of class can be inherited. In PHP 5, interfaces may declare only methods. An interface cannot declare any variables. To extend from an Interface, keyword implements is used. PHP5 supports class extending more than one interface.

Syntax:

interface interfacename
{
	function name()
	function name1()
}
 
class temp implements interfacename
{
	public function name
	{
 
	}
}

Example1 – Interface Class in PHP5

< ?
interface employee
{
	function setdata($empname,$empage);
	function outputData();
}
 
class Payment implements employee
{
	function setdata($empname,$empage)
	{
            //Functionality
	}
 
	function outputData()
	{
		echo "Inside Payment Class";
	}
}
 
$a = new Payment();
$a->outputData();
?>


Output: “Inside Payment Class”
Explanation for the above Example:

Here i have a interface class called employee having two methods setData() and outputData()

I am implementing the interface class on Payment class. Payment class also contains the functionality for the methods defined in the interface.

Example2 – Combining Abstract Class and Interface Class

< ?
interface employee
{
	function setdata($empname,$empage);
	function outputData();
}
 
abstract class Payment implements employee //implementing employee  interface
{
	abstract function PaymentInfo();
}
 
class PaySlip extends Payment
{
	function collectPaySlip()
	{
		echo "PaySlip Collected";
		$this->outputData();
	}
 
	function outputData()
	{
		echo "Inside PaySlip Class";
	}
 
	function PaymentInfo()
	{
		echo "Inside PaySlip Class";
	}
 
	function setData($empname,$empage)
	{
             //Functionality
	}
}
$a = new PaySlip();
$a->collectPaySlip();
?>

Output:
PaySlip Collected
Inside Payment Class



Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: PHP Tags: , ,
  1. November 5th, 2007 at 07:08 | #1

    Good article, especially when you begin to introduce patterns in to your PHP programming, interfaces will be an integral part!

  2. GDizzle
    December 4th, 2007 at 14:56 | #2

    What does the S stand for in OOPS?

  3. December 7th, 2007 at 02:42 | #3

    Hi GDizzle,
    S stands for “Systems”

  4. suyash
    January 15th, 2008 at 03:57 | #4

    Good tutorials

  5. April 2nd, 2008 at 02:33 | #5

    found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..

  6. Hamish
    December 9th, 2008 at 21:30 | #6

    Interfaces can declare constant variables. Thought that it’s worth mentioning…

  7. Sagar
    December 25th, 2008 at 05:02 | #7

    This is very useful example for me .
    And also this is good article.
    I like it.

  8. January 8th, 2009 at 22:45 | #8

    Through Interface we can overcome space of multiple inheritance.

  9. OneSpace
    July 3rd, 2009 at 15:14 | #9

    Little Typping-Error @ Example2:

    “< ?" should be "<?".

    But nice Tutorial…

  10. July 28th, 2009 at 02:52 | #10

    Nice tutorial .. Thanks guys
    Nice tutorial .. Thanks guys
    Nice tutorial .. Thanks guys
    Nice tutorial .. Thanks guys

  11. August 7th, 2009 at 07:32 | #11

    Great tutorial on class interfacing!

  12. September 9th, 2009 at 03:49 | #12

    This tutorial is enough for me for this time.I want some more.. like the main feature of interface multiple inheritance’s exapmle..

  13. November 2nd, 2009 at 06:48 | #13

    good tutorial to work with Interface in php

  14. January 18th, 2010 at 07:14 | #14

    good tutorial to work with Interface in php

  15. January 19th, 2010 at 08:03 | #15

    thanks your post

  16. awesemo
    February 25th, 2010 at 20:35 | #16

    Correct my if im wrong, interface is not a class.

  17. April 18th, 2010 at 00:04 | #17

    @awesemo
    Interface is not an class it defines an contract that class has to fulfill.

  18. October 5th, 2010 at 14:15 | #18

    Thanks! Very good article! Cheers!

  19. hmhb
    November 14th, 2010 at 23:29 | #19

    I can’t understand the main advantage of creating interfaces and implementing them into our class

    • November 14th, 2010 at 23:39 | #20

      Hi HMHB,
      Treat Interface as pure abstract class, the main advantage interface holds is as follows:
      - You can define constants that will never change within an application. As all constants are static and final.
      - You define methods inside Interface class that concrete class has to fulfill, e.g. Car can be Interface Class that has startEngine() methods. Now you can write Truck class which implements Car Interface and so as per the contract Truck Class has to write the logic for startEngine() method, now the logic inside startEngine() will be specific to Truck. Similarly you can have other class like SUV, Tempo etc
      * The above example emphasis on concept of re-usability such that every Car needs to have startEngine() method which is mandatory. The developer need not to remember the methods required for Truck, just implements Car interface and compiler will prompt error if some method is not implemented.

      Hope this would make you understand why interface are required

      Thanks,
      Hitesh Agarwal

  20. hmhb
    November 14th, 2010 at 23:33 | #21

    I don’t know where and when to use the enterface…

  1. September 14th, 2010 at 10:52 | #1