2008年7月24日星期四

Re: [fw-mvc] Best practices in a thin-controller application

Hi David,

David Mintz wrote:
> OK, what about
>
> class MyModel {
>
> protected $table;
> protected $row;
>
> function __construct ($id = null) {
>
> $this->table = new My_Subclass_Of_Zend_Db_Table_Abstract();
> if ($id) { // pretend you validated, for brevity's sake
> $this->row = $this->table->find($id)->current();
> if (!$this->row) throw new Exception("d'oh!"); // or throw
> your subclass
> } else {
> $this->row = $this->table->createRow();
> }
> }

This is a perfectly valid thing to do. Basically, you're saying that
even when you accept a null id, the resulting object is still created
with a valid state. Some might argue that because the object's semantics
are slightly different, due to its two different types of valid state,
it could be confusing. However, in all practicality it doesn't really
matter as long as it's consistently applied throughout the application.

Regards,
Bryce Lohr


August 1st is National Minority Organ Donor Awareness Day

没有评论: