3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* README ------ Below there are many classes, but you should focus only one class: Application, and particularly its '__construct' method. There are also libraries (AuthorCollection, BookCollection, etc) that you can freely use to complete the mission. Your mission, should you choose to accept it, is to add codes _only_ in the sections labeled "PLACE YOUR CODE HERE" in order to: 1. Make this file frees from any error when run with PHP 5 interpreter, and 2. Make the resulting application prints as many "true"s as possible. That is, the expected result is something like this: 1 : true 2 : true 3 : true 4 : true 5 : true 6 : true 7 : true 8 : true 9 : true 10 : true */ /** * Watch and read carefully about all description source docs inside Application class. * You can put your code inside blocks with "PLACE YOUR CODE HERE" signs! * * Classes that cannot be changed: * 1. Application * 2. All collection libraries */ /** * DO NOT CHANGE ANY CODES INSIDE 'APPLICATION' CLASS! */ class Application { public $FictionBookLibrary; public $availableBooks; public $booksByArthurConanDoyle; public $john; public $jane; public $borrowedByJohn; public $borrowedByJane; /* * list of book titles */ public $harryPotterAndSorcerersStone; public $harryPotterAndChamberOfSecrets; public $theFellowshipOfTheRing; public $theTwoTowers; public $theAdventuresOfSherlockHolmes; public $aStudyInScarlet; public $anotherTheAdventuresOfSherlockHolmes; private $debugged; public function __construct($debug = true) { //flag to show verbose message into command line $this->debugged = $debug; //////////////////////////////////////////////// // The following story is totally fiction. Any resemblances with real world character is (perhaps) // coincidence. /* Bill Gates, the ambitious book collector. ----------------------------------------- */ /* Many people knows him as the founder and ex-CEO of one of the largest software company in the world, but not many knows that he's also an avid book collector. In fact, in one interview, he said that the reason he build his own company is that so he can build his own book library. */ /* Now the time has come, he became one of the richest man in the planet. "I've been waiting for this for a really long, long time". Then he start building his own fiction book library. */ $this->FictionBookLibrary = new FictionBookLibrary; /* He then go round the world. People know him go around the world because he's trying to cure malaria, to do philanthropic things, and whatnot. But little did they know that Bill is also collecting fiction books for his library. Travelling around the world, he found the rarest kind of fiction books he wants to collect, like 'Harry Potter' series by 'J.K. Rowling', 'The Lord of the Rings' series by 'J.R.R Tolkien', and 'Sherlock Holmes' series by 'Arthur Conan Doyle'. */ $this->harryPotterAndSorcerersStone = new Book("Harry Potter and the Sorcerer's Stone", "J.K. Rowling"); $this->harryPotterAndChamberOfSecrets = new Book("Harry Potter and the Chamber of Secrets", "J.K. Rowling"); $this->theFellowshipOfTheRing = new Book("The Fellowship of the Ring", "J.R.R. Tolkien"); $this->theTwoTowers = new Book("The Two Towers", "J.R.R. Tolkien"); $this->theAdventuresOfSherlockHolmes = new Book("The Adventures of Sherlock Holmes", "Arthur Conan Doyle"); $this->aStudyInScarlet = new Book("A Study in Scarlet", "Arthur Conan Doyle"); /* Not wanting to waste his time, Bill bought several of those rare books. (Have I tell you that he has a lot of money? I mean, a really really lot of money?) The 'Harry Potter' series, for example, he bought 5 of those (2 books for 'Harry Potter and the Sorcerer's Stone', and 3 for 'Harry Potter and Chamber of Secrets'). 'The Lord of the Rings' series, on the other hand, proven to be quite a challenge even for Bill, he only got 3 of those (1 for 'The Fellowship of the Rings' and 2 for 'The Two Towers'). Luckily, for 'Sherlock Holmes' series, he found and bought many of those (20 and 10 books for 'The Adventures of Sherlock Holmes' and 'A Study in Scarlet', respectively.) */ $this->FictionBookLibrary->addCollection($this->harryPotterAndSorcerersStone, 2); $this->FictionBookLibrary->addCollection($this->harryPotterAndChamberOfSecrets, 3); $this->FictionBookLibrary->addCollection($this->theFellowshipOfTheRing, 1); $this->FictionBookLibrary->addCollection($this->theTwoTowers, 2); $this->FictionBookLibrary->addCollection($this->theAdventuresOfSherlockHolmes, 20); $this->FictionBookLibrary->addCollection($this->aStudyInScarlet, 10); /* Bill really loves his book collection, he always list its collection every day. */ $this->availableBooks = $this->FictionBookLibrary->listAvailableBooks(); $this->verbose("1 : " . (int) $this->availableBooks->contains($this->harryPotterAndChamberOfSecrets)); $this->verbose("2 : " . (int) $this->availableBooks->contains($this->theFellowshipOfTheRing)); $this->verbose("3 : " . (int) $this->availableBooks->contains($this->theAdventuresOfSherlockHolmes)); /* Of all the fiction writer that he loves, he adores 'Arthur Conan Doyle' the most. In fact, when he couldn't sleep in the middle of night, he often goes to his library just to check the books that authored by him. He said this gives him peace required for him to sleep. */ $this->booksByArthurConanDoyle = $this->FictionBookLibrary->listBooksAuthoredBy("Arthur Conan Doyle"); $this->verbose("4 : " . (int) $this->booksByArthurConanDoyle->contains($this->theAdventuresOfSherlockHolmes->title)); $this->verbose("5 : " . (int) $this->booksByArthurConanDoyle->contains($this->aStudyInScarlet->title)); /* Did I mention that he's also really generous person? Bill tries to open membership into his precious library. He wants people to be inspired by those fiction books, just like him. However, he don't want public know about this (it's still his precious, after all), so he open the membership just for two people: John and Jane. */ $this->john = $this->FictionBookLibrary->createMember("John"); $this->jane = $this->FictionBookLibrary->createMember("Jane"); /* Really happy to be a member of Bill's library, John immediately borrows the first series of 'Harry Potter' and 'The Lord of the Rings'. */ $this->john->borrow($this->harryPotterAndSorcerersStone); $this->john->borrow($this->theFellowshipOfTheRing); /* Bill knows about this, of course. Somehow, it makes him feel happy. He feels like his life is now complete. */ $this->borrowedByJohn = $this->john->listBorrowedBooks(); $this->verbose("6 : " . (int) $this->borrowedByJohn->contains($this->harryPotterAndSorcerersStone)); $this->verbose("7 : " . (int) $this->borrowedByJohn->contains($this->theFellowshipOfTheRing)); /* The other member, Jane, is a hardcore fan of 'The Lord of the Rings'. The next morning, she tries to borrow two 'The Fellowship of The Ring' and 'The Two Towers' from the series, .. */ $this->jane->borrow($this->theFellowshipOfTheRing); $this->jane->borrow($this->theTwoTowers); /* .. But little did she know that 'The Fellowship of the Ring' already lent. "Well", she thought, "at least I got 'The Two Towers'". */ $this->borrowedByJane = $this->jane->listBorrowedBooks(); $this->verbose("8 : " . (int) !$this->borrowedByJane->contains($this->theFellowshipOfTheRing)); $this->verbose("9 : " . (int) $this->borrowedByJane->contains($this->theTwoTowers)); /* Bill, who really cares about his customers, feels sad about this. "I should find more books", he thought. Later did he know that it will be really reallyh ard for him to find more rare fiction book. Fast forward ten years. Bill arrived at Krakhosia, a country that most people never heard of. He got there without any intention finding a rare fiction book. That's why his eye bewildered when he encountered another copy of 'The Adventures of Sherlock Holmes', .. */ $this->anotherTheAdventuresOfSherlockHolmes = new Book("The Adventures of Sherlock Holmes", "Arthur Conan Doyle"); /* .. But because of his old age, he can't remember whether he already has it in his library, and so he checks.. */ $this->verbose("10 : " . (int) $this->availableBooks->contains($this->anotherTheAdventuresOfSherlockHolmes)); /* "Seems I had it already. Sigh..", he said. "Perhaps another time".. */ } /** * print message into command line */ private function verbose($string) { if ($this->debugged) { print "\n".$string; } } } /* ================ LIBRARY =================================== */ /* DO NOT CHANGE ANY CODE INSIDE ALL LIBRARY CLASSES */ class Collection { protected $collections = []; public function add() { //pass } public function contains($key) { //pass } public function all() { return $this->collections; } } class AuthorCollection extends Collection { public function add(BookEntity $key) { $this->collections[$key->author][] = $key->title; } public function contains($key) { return array_key_exists($key, $this->collections); } public function get($key) { return $this->collections[$key]; } } class BookCollection extends Collection { public function add(BookEntity $book, $total) { $this->collections[$book->title] = $total; } public function contains(BookEntity $book) { return array_key_exists($book->title, $this->collections); } public function decrease($book) { $prev = $this->collections[$book->title]; $now = (int) $prev - 1; $this->collections[$book->title] = $now; } public function getStock($book) { return $this->collections[$book->title]; } } class BorrowedCollection extends Collection { public function add(BookEntity $book) { $this->collections[] = $book->title; } public function contains(BookEntity $book) { return in_array($book->title, $this->collections); } } class SingleCollection extends Collection { public function add($key) { $this->collections[] = $key; return $this; } public function contains($book) { return in_array($book, $this->collections); } } /* ============================================================ */ /*================= YOUR CODE HERE ============================ */ class FictionBookLibrary { /* PLACE YOUR CODE HERE */ public $bookCollection; public $authorCollection; public $members; public function __construct() { $this->bookCollection = new BookCollection(); $this->authorCollection = new AuthorCollection(); class_alias('Book', 'BookEntity'); } public function addCollection(BookEntity $book, $total) { $this->bookCollection->add($book, $total); $this->authorCollection->add($book); } public function listAvailableBooks() { return $this->bookCollection; } public function listBooksAuthoredBy($author) { $collections = new SingleCollection(); foreach ($this->authorCollection->get($author) as $title) { $collections->add($title); } return $collections; } public function createMember($name) { $this->members[] = new Member($name, $this); return end($this->members); } } class Book { /* PLACE YOUR CODE HERE */ public $title; public $author; public function __construct($title, $author) { $this->title = $title; $this->author = $author; } } class Member { /* PLACE YOUR CODE HERE */ public $name; public $borrowedCollection; public $library; public function __construct($name, $library) { $this->name = $name; $this->borrowedCollection = new BorrowedCollection(); $this->library = $library; } public function borrow(BookEntity $book) { if($this->library->bookCollection->getStock($book) > 0) { $this->library->bookCollection->decrease($book); $this->borrowedCollection->add($book); } } public function listBorrowedBooks() { return $this->borrowedCollection; } } /* ============================================================ */ $app = new Application;
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Declaration of AuthorCollection::add(BookEntity $key) must be compatible with Collection::add() in /in/ivohn on line 247
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
87.5 ms | 401 KiB | 8 Q