Interface Classes
If you have done some programming before you may of seen the interface class be used before. The interface class is common in many design patterns. It will not add any additional functionality but it outlines a standard format to which your classes need to use. Any classes which inherit from an interface class must have at least the same methods as the interface class. The methods placed in the interface class will not have any functionality but will have just the method stubs to be used by the other classes. The methods outlined in the interface must be used by the other classes or it will throw an error.
Defining An Interface Class
Interface classes are defined by using the keyword interface. All the methods in the interface must be public as this is the nature of an interface.
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
Using A Defined Interface Class
You will use an interface on classes when you have classes which will do similar things but the functionality being different, which is explained in the tutorial below. To use an interface class it's done by using the implements keyword.
class Template implements iTemplate
{
public function setVariable($name, $var){
//functionality
}
public function getHtml($template){
//functionality
}
}
As you can see from the above example the Template class has both the functions defined in the interface if it is missing a function then it will cause an error to be thrown.
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
Implement the interface
This will work class Template implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } } // This will not work // Fatal error: Class BadTemplate contains 1 abstract methods // and must therefore be declared abstract (iTemplate::getHtml) class BadTemplate implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } } ?>
Abstract Classes
If you are aware of an interface class then you will need to use an abstract class at some point. An abstract class is a type of class which we can not create an object from. An abstract class is used like an interface class except we can add functionality into the methods defined in the abstract class.
To use the abstract class we will also need to use the extends keyword, we can only implement one abstract class where we can implement multiple interface classes. An abstract class will have abstract methods which are defined by the abstract keyword, these methods are like the methods defined in the interface classes. All methods in abstract classes that you want to override can not be private methods as they will need to be used outside of the class. You will use an abstract class in a similar way you would use an interface but there is common functionality which will be used on all the classes which extend it.
<?php
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
?>