Home > PHP > OOPS in PHP 5 Tutorial – 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();
?>

Your email:

 


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


Custom Search

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.

  1. No trackbacks yet.