Custom Search

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

Similar Posts

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

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

What does the S stand for in OOPS?

Hi GDizzle,
S stands for “Systems”

Good tutorials

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 ..

Leave a comment

(required)

(required)