Wednesday, April 1, 2009

Object interface example in php

Think about your laptop or computer, you did not interact with the things that is inside within it. To on your machine you press the power button. Similarly you interact with your machine through mouse, keyboard etc. The methods through which you interact with the object togetherly forms an interface.

You can define an interface in this way. An interface is the group of related methods without bodies. Details implementation of the interface will be within classes that implements the interface.

Note that, all methods in the interface must be public as by it's nature and if a class implements an interface all the methods must be defined within the class. Interface methods does not tell how it will work that is details implementation of interface methods must be handle within the class that implements the interface; how that class will use the method.

You can't create instance of an interface. Interface can be subclassed.

To create an interface you have to use interface keyword followed by the curly brace. Methods declaration will be inside the curly brace. To implements an interface you have to use implements keyword with the class definition that will implement the interface.

Below is an example of using interface.

<?php
interface a{
public function method1();
}
interface b{
public function method2();
}
interface c extends a,b{
public function method3($name);
}
class d implements c{
public function method1(){
}
public function method2(){
}
public function method3($name){
}
}

?>

Related Documents
http://arjudba.blogspot.com/2009/03/abstract-class-and-abstract-method-with.html
http://arjudba.blogspot.com/2009/03/static-declaration-with-example-in-php.html
http://arjudba.blogspot.com/2009/03/class-concepts-and-usage-syntax-in-php.html
http://arjudba.blogspot.com/2009/03/class-inheritance-example-in-php.html
http://arjudba.blogspot.com/2009/03/autoloading-objects-in-php.html
http://arjudba.blogspot.com/2009/03/constructors-and-destructors-in-php.html

No comments:

Post a Comment