. PHP Design Patterns . PHP Design Patterns PHP OO Interface Basics
PHP Design Patterns PHP OO Interface Basics
PHP Interface Basics
Here is an example of creating two very simple interfaces class called OOPHPInterfaceOne and OOPHPInterfaceTwo, along with OOPHPClassToImplementInterfaces which implements both.
Note that to successfully implement OOPHPInterfaceOne and OOPHPInterfaceTwo, OOPHPClassToImplementInterfaces must have getNameOne(), getnameTwo(), setNameOne(), setNameTwo() functions().
OOPHPClassToImplementInterfaces.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
//this class "extends" OOPHPInterfaceOne and OOPHPInterfaceTwo.
//Unlike abstracts, of which only one can be extended,
// a class can implement multiple interfaces.
include_once('OOPHPInterfaceOne.php');
include_once('OOPHPInterfaceTwo.php');
class OOPHPClassToImplementInterfaces
implements OOPHPInterfaceOne, OOPHPInterfaceTwo {
private $instanceNameOne;
//OOPHPInterfaceOne has the function getNameOne,
// so we must implement it here.
public function getNameOne() {
return $this->instanceNameOne;
}
//OOPHPInterfaceTwo has the function getNameTwo,
// so we must implement it here.
public function getNameTwo() {
return $this->instanceNameTwo;
}
//OOPHPInterfaceOne has the function setNameOne,
// so we must implement it here.
public function setNameOne($nameIn) {
$this->instanceNameOne = $nameIn;
}
//OOPHPInterfaceTwo has the function setNameTwo,
// so we must implement it here.
public function setNameTwo($nameIn) {
$this->instanceNameTwo = $nameIn;
}
}
download source, use right-click and "Save Target As..." to save with a .php extension.
OOPHPInterfaceOne.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
//OOPHPInterfaceOne - a simple OO PHP Interface
// this defines two functions, getNameOne() and setNameOne($nameIn)
// which any class extending this must have
interface OOPHPInterfaceOne {
function getNameOne();
function setNameOne($nameIn);
}
download source, use right-click and "Save Target As..." to save with a .php extension.
OOPHPInterfaceTwo.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
//OOPHPInterfaceTwo - a simple OO PHP Interface
// this defines two functions, getNameTwo() and setNameTwo($nameIn)
// which any class extending this must have
interface OOPHPInterfaceTwo {
function getNameTwo();
function setNameTwo($nameIn);
}
download source, use right-click and "Save Target As..." to save with a .php extension.
testOOPHPInterface.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
include_once('OOPHPClassToImplementInterfaces.php');
define('BR', '<'.'BR'.'>');
echo 'BEGIN TESTING PHP INTERFACES'.BR;
echo BR;
echo 'test 1 - create a class which implements two interfaces'.BR;
$classOne = new OOPHPClassToImplementInterfaces();
echo BR;
$classOne->setNameOne("Harold");
echo 'Name One: '.$classOne->getNameOne();
echo BR;
$classOne->setNameTwo("Maude");
echo 'Name Two: '.$classOne->getNameTwo();
echo BR.BR;
echo 'END TESTING PHP INTERFACES'.BR;
download source, use right-click and "Save Target As..." to save with a .php extension.
output of testOOPHPInterface.php
BEGIN TESTING PHP INTERFACES
test 1 - create a class which implements two interfaces
Name One: Harold Name Two: Maude
END TESTING PHP INTERFACES
References
Design Patterns
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesPHP 5
The Official PHP web siteCore PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
| Sign In |
| to add the first comment for PHP Design Patterns PHP OO Interface Basics. |