understanding object design - help!

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

understanding object design - help!

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Using another class and an array in the Task class.
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Expose through accessors, maybe, but not directly exposed.
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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. :)
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You could supply an iterator to the note array and/or facilitate naming the notes in some fashion.
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

Post 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
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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();
}

?>
daveUK
Forum Newbie
Posts: 9
Joined: Thu Jan 25, 2007 9:00 am
Location: UK

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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. :)
Post Reply