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:
- OOPS in PHP 5 – __destruct() Method
- OOPS in PHP 5 Tutorial – Abstract Class
- Reading Remote URL HTML Source in PHP
- Reading Excel Documents from PHP applications
- PHP5 Tutorial – __sleep() Magic Method
- PHP5 Tutorial – __call() Magic Method
- OOPS in PHP 5 – Polymorphism
- PHP5 Tutorial – __toString() Magic Method
- Writing HTML Scrapper in PHP
- OOPS in PHP 5 Tutorial – Static Keyword
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.
Thanks! Very good article! Cheers!
I can’t understand the main advantage of creating interfaces and implementing them into our class
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
I don’t know where and when to use the enterface…