Answer the question
In order to leave comments, you need to log in
What does it mean to pass an interface as a parameter?
It's not the first time I've seen code like
public function save(ModelInterface $model)
{
$modelName = $this->getModelName();
$modelEvent = new ModelEvent($model);
. . .
Answer the question
In order to leave comments, you need to log in
something like this ... if the interface is in the parameter, then we can pass any instance of the class that implements the specified interface there.
interface Berry {
public function getSugar() {};
}
class Strawberries implements Berry {
public function getSugar() {
return 20;
}
}
class Watermelon implements Berry {
public function getSugar() {
return 50;
}
}
class Omnomnom() {
public function printBerrySugar(Berry $berry) {
return print($berry->getSugar());
}
public function test() {
$this->printBerrySugar(new Strawberries ());
$this->printBerrySugar(new Watermelon ());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question