What is implementing a function? normally, what I know is implementing an interface which has
function foo();
and implementing like
function foo($bar)
in another class
but what happens when I implement the function in the interface? do I have to reimplement them in the subclasses? or can I use them in subclasses directly, assuming they are implemented in the subclasses.
interface ibarbar{
function foo($bar)
{
return $bar.$bar;}
}
class barbar implements ibarbar
{
function baz()
{
$this->barbar(); //used without implementing in subclass. expected results unknown.
}
}
PHP documentation says
// 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;
}
}
so all methods in an interface are abstract methods. but I couldn’t find how to extend a function. or implement a function
You can’t implement a function in an interface. You can only specify its signature.
If you want to specify signatures for some methods but provide implementation for others, use an abstract class: