Skip to content

Instantly share code, notes, and snippets.

@jake-yeg
Created February 7, 2017 22:52
Show Gist options
  • Save jake-yeg/2739349d71fe58f8739396229e516f81 to your computer and use it in GitHub Desktop.
Save jake-yeg/2739349d71fe58f8739396229e516f81 to your computer and use it in GitHub Desktop.
<?php
/*
Challenge 2: Implement AnswerInterface and get Question to echo "4".
*/
class Question
{
public function __construct(AnswerInterface $answer)
{
echo "What is 2 + 2?\n";
$answer = $answer->get()->the()->answer();
if ($answer instanceof AnswerInterface) {
echo $answer . PHP_EOL;
}
}
}
interface AnswerInterface
{
public function get();
public function the();
public function answer();
}
// start editing here
class Answer implements AnswerInterface{
private $theanswer;
public function __construct(){}
public function get(){
$this->theanswer = 2;
return $this;
}
public function the(){
$this->theanswer += 2;
return $this;
}
public function answer(){
return $this;
}
public function __toString() {
return (string)$this->theanswer;
}
}
// end editing here
$answer = new Answer;
$question = new Question($answer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment