I am currently studying towards my Zend PHP certification and have came across this question in my first practice exam:
Consider the following series of classes and interfaces
Code:
<?php
interface MyInterface {
public function getTitleAndAuthor();
}
class MyClass {
private $_author;
private $_title;
public function getAuthor() {
return $this->_author;
}
public function getTitle() {
return $this->_title;
}
}
class MyClass2 {
private $_instance;
public function __construct(MyClass $a) {
$this->_instance = $a;
}
public function getTitleAndAuthor() {
$retval = array(
'title' => $this->_instance->getTitle(),
'author' => $this->_instance->getAuthor()
);
return $retval;
}
}
class MyClass3 {
public function doSomething(MyInterface $instance) {
$value = $instance->getTitleAndAuthor();
}
}
?>
Can anyone advise - MyClass2 is an example of the what design pattern, and why?
Best Regards,
Picco