understanding object design - help!
Moderator: General Moderators
understanding object design - help!
I'm writing a conversion of a Palm PDA timesheet application, but I am new to OO in php5
I have started the basics by writing a task class (used to define a task to which time is recorded against). It contains properties such as taskName, taskStartDate, taskEndDate & description. I have several methods for adding a task, calculating rates/totals, etc...
The main problem that I'm trying to understand is...
I want to be able to add multiple notes to a task, e.g. developer comments, which can be added/edited/deleted at any time.
How would you recommend I add this to the class?
TIA
Dave
I have started the basics by writing a task class (used to define a task to which time is recorded against). It contains properties such as taskName, taskStartDate, taskEndDate & description. I have several methods for adding a task, calculating rates/totals, etc...
The main problem that I'm trying to understand is...
I want to be able to add multiple notes to a task, e.g. developer comments, which can be added/edited/deleted at any time.
How would you recommend I add this to the class?
TIA
Dave
Ok I understand this. However, I'm still struggling with how I would handle the calling of the notes class.feyd wrote:Expose through accessors, maybe, but not directly exposed.
Can you recommend any example code that does this kind of thing? Sorry I have a complete mental block with this.
Thanks
Dave
Code: Select all
$task = new Task(/* blah */);
$note = new Note(/* blah */);
$task->addNote($note);
$note->updateComment('dum de dum');
$task->addNote(new Note(/* blah */));
//etcThat's cool! But how would I list multiple notes and allow editing of any note returned?Jenk wrote:All objects are passed by reference, so the changes made to $note post addition to $task will still be valid.Code: Select all
$task = new Task(/* blah */); $note = new Note(/* blah */); $task->addNote($note); $note->updateComment('dum de dum'); $task->addNote(new Note(/* blah */)); //etc
Sorry for being a tad thick on this.
Dave
Ok, so I've created a method within the task classfeyd wrote:You could supply an iterator to the note array and/or facilitate naming the notes in some fashion.
public function addNote(Timesheet_Note $note) {
$notesArray[] = $note;
}
and I have a public property exposed called $notesArray, defined as $this->notesArray = array(); in the constructor.
I add the note using:
$myTask->addNote(new Timesheet_Note("my comment"));
But on printing the array using: print_r($myTask->notesArray);
it says: Array ( )
Why?
Is it possible to do: $myTask->notesArray[0]->comment; ?
Thanks
Dave
A further improvement could be to have a method return an iterator object of all notes within the task:
Code: Select all
class Task
{
private $notesArray = array();
/* snip.. */
public function getNotesIterator()
{
return new Iterator($this->notesArray());
}
}
$task = new Task;
/* snip */
$it = $task->getNotesIterator();
while ($it->next())
{
echo $it->current()->getComment();
}
?>Jenk wrote:A further improvement could be to have a method return an iterator object of all notes within the task:
Code: Select all
class Task { private $notesArray = array(); /* snip.. */ public function getNotesIterator() { return new Iterator($this->notesArray()); } } $task = new Task; /* snip */ $it = $task->getNotesIterator(); while ($it->next()) { echo $it->current()->getComment(); } ?>
Ooo, that looks nice. I'm assuming that Iterator isn't an inbuilt class of PHP? do you have a recommended Iterator class?
Thanks
There is an iterator as part of the Standard PHP Library
But if that is not your flavor, it's trivial to make your own.
But if that is not your flavor, it's trivial to make your own.