/* Factory Pattern */
class Book {
private $book_name;
private $book_author;
const BR = '<br>';
public function __construct($name, $author) {
$this->book_name = $name;
$this->book_author = $author;
}
public function getNameAndAuthor() {
return $this->book_name . ' - ' . $this->book_author . self::BR;
}
}
class BookFactory {
public static function create($name, $author) {
return new Book($name, $author);
}
}
$book1 = BookFactory::create('Understanding Cats', 'Sir Meow');
$book2 = BookFactory::create('Why is my sister naked?', 'Dr. Phil Eh Buster');
echo "<b>Book List</b><br>\n";
echo $book1->getNameAndAuthor();
echo $book2->getNameAndAuthor();