The simplest option (I'll show a better way in just a bit) is to create a global history object.
PHP Code:
$history = new History();
class Tasks
{
public function updateTask()
{
global $history;
$history->setHistory();
}
}
The downside, though, is that by relying on a global variable, the Tasks class is tightly coupled to this particular application environment, and it's also harder to create different task objects that might have different histories. A better way is to inject a history object into your instantiated tasks object.
PHP Code:
class Tasks
{
private $history;
public function __construct(History $history)
{
// Tasks doesn't need to know where this history object came from or how it was created
// It only needs to know that this is the object it should use
$this->history = $history;
}
public function updateTask()
{
$this->history->setHistory();
}
}
$history = new History();
$tasks = new Tasks($history);
Bookmarks