note 115451 deleted from language.oop5.interfaces by salathe
| From: | salathe@php.net | Date: | Mon, 04 Aug 2014 08:48:53 +0000 |
| Subject: | note 115451 deleted from language.oop5.interfaces by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200105@lists.php.net to get a copy of this message | ||
Note Submitter: serverdigital at gmail dot com
----
Hi guys, let me show a very useful example of interface to use in PHP, can found more information at
http://code.tutsplus.com/tutorials/solid-part-4-the-dependency-inversion-principle--net-36872
class Test extends PHPUnit_Framework_TestCase {
function testItCanReadAPDFBook() {
$b = new PDFBook();
$r = new EBookReader($b);
$this->assertRegExp('/pdf book/', $r->read());
}
function testItCanReadAMobiBook() {
$b = new MobiBook();
$r = new EBookReader($b);
$this->assertRegExp('/mobi book/', $r->read());
}
}
interface EBook {
function read();
}
class EBookReader {
private $book;
function __construct(EBook $book) {
$this->book = $book;
}
function read() {
return $this->book->read();
}
}
class PDFBook implements EBook {
function read() {
return "reading a pdf book.";
}
}
class MobiBook implements EBook {
function read() {
return "reading a mobi book.";
}
}