Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Last active December 18, 2015 07:49
Show Gist options
  • Select an option

  • Save ackintosh/5748987 to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/5748987 to your computer and use it in GitHub Desktop.
test the time that book has been loaned.
<?php
class Loan
{
private $book;
private $time;
private static $faketime;
public function __construct(Book $book)
{
$this->book = $book;
$this->time = self::$faketime ? : new DateTime();
}
public static function setFaketime(DateTime $faketime)
{
self::$faketime = $faketime;
}
public function toString()
{
return $this->book->title() . ' loaned on ' . $this->time->format('Y/m/d H:i:s');
}
}
class Book
{
private $title;
public function __construct($title) { $this->title = $title; }
public function title() { return $this->title; }
}
Loan::setFaketime(new DateTime('2013/06/10 18:00:00'));
$l = new Loan(new Book('WEEKLY JUMP'));
echo $l->toString(); // always returns 'WEKLY JUMP loaned on 2013/06/10 18:00:00'
<?php
class Loan
{
// ....
public function __construct($book, $time)
{
$this->book = $book;
$this->time = $time;
}
// ....
}
<?php
class Loan
{
private $book;
private $time;
public function __construct($book)
{
$this->book = $book;
$this->time = new DateTime();
}
public function toString()
{
return $this->book->title() . ' loaned on ' . $this->time->format('Y/m/d H:i:s');
}
}
<?php
class Loan
{
// ....
public function __construct($book, $time, $num)
{
$this->book = $book;
$this->time = $time;
$this->num = $num;
}
// ....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment