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:
- PHP5 Tutorial – __isset() Magic Method
- Increase PHP Script Memory Limit
- MySql Batch Insert Using PHP
- Writing HTML Scrapper in PHP
- OOPS in PHP 5 Tutorial – Abstract Class
- How to Implement Text to Speech in PHP
- HTTP FORM POST in PHP using AJAX
- PHP5 Tutorial – __wakeup() Magic Method
- OOPS in PHP 5 Tutorial – Static Keyword
- OOPS in PHP 5 – Polymorphism



































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 ..
Interfaces can declare constant variables. Thought that it’s worth mentioning…
This is very useful example for me .
And also this is good article.
I like it.
Through Interface we can overcome space of multiple inheritance.
Little Typping-Error @ Example2:
“< ?" should be "<?".
But nice Tutorial…
Nice tutorial .. Thanks guys
Nice tutorial .. Thanks guys
Nice tutorial .. Thanks guys
Nice tutorial .. Thanks guys
Great tutorial on class interfacing!
This tutorial is enough for me for this time.I want some more.. like the main feature of interface multiple inheritance’s exapmle..
good tutorial to work with Interface in php
good tutorial to work with Interface in php
thanks your post
Correct my if im wrong, interface is not a class.
@awesemo
Interface is not an class it defines an contract that class has to fulfill.