Page 1 of 1
understanding object design - help!
Posted: Thu Jan 25, 2007 9:16 am
by daveUK
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
Posted: Thu Jan 25, 2007 9:24 am
by feyd
Using another class and an array in the Task class.
Posted: Thu Jan 25, 2007 9:49 am
by daveUK
feyd wrote:Using another class and an array in the Task class.
So expose an array as a property on the Task class to accept type note? Is this what you mean?
Thanks
Dave
Posted: Thu Jan 25, 2007 10:00 am
by feyd
Expose through accessors, maybe, but not directly exposed.
Posted: Thu Jan 25, 2007 10:29 am
by daveUK
feyd wrote:Expose through accessors, maybe, but not directly exposed.
Ok I understand this. However, I'm still struggling with how I would handle the calling of the notes class.
Can you recommend any example code that does this kind of thing? Sorry I have a complete mental block with this.
Thanks
Dave
Posted: Thu Jan 25, 2007 10:37 am
by Jenk
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
All objects are passed by reference, so the changes made to $note post addition to $task will still be valid.

Posted: Thu Jan 25, 2007 10:43 am
by daveUK
Jenk wrote: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
All objects are passed by reference, so the changes made to $note post addition to $task will still be valid.

That's cool! But how would I list multiple notes and allow editing of any note returned?
Sorry for being a tad thick on this.
Dave
Posted: Thu Jan 25, 2007 11:03 am
by feyd
You could supply an iterator to the note array and/or facilitate naming the notes in some fashion.
Posted: Fri Jan 26, 2007 4:16 am
by daveUK
feyd wrote:You could supply an iterator to the note array and/or facilitate naming the notes in some fashion.
Ok, so I've created a method within the task class
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
Posted: Fri Jan 26, 2007 5:48 am
by daveUK
Sorry, just realised what I've done.
I specified the following:
public function addNote(Timesheet_Note $note) {
$notesArray[] = $note;
}
Should have been:
public function addNote(Timesheet_Note $note) {
$this->notesArray[] = $note;
}
Cheers
Dave
Posted: Fri Jan 26, 2007 8:47 am
by Jenk
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();
}
?>
Posted: Fri Jan 26, 2007 9:32 am
by daveUK
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
Posted: Fri Jan 26, 2007 11:04 am
by Jenk
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.
