Reply to Re: [PHP] Suggestions for class design

Your name:

Reply:


Posted by Jochem Maas on 09/20/05 11:44

Hi Chris,

nice thread, good questions - nice to see some real programming
theory being discussed - does us all some good :-)

here is my take, hth:

Chris W. Parker wrote:
> Anas Mughal <mailto:anasmughal@gmail.com>
> on Monday, September 19, 2005 4:02 PM said:
>
>
>>The simplest way to solve this problem is as follows:
>>
>>- Have your Customer class hold only attributes for a customer. This
>>class would only have getter and setter methods. In the Java world,
>>this is referred to as a JavaBean.
>>- Then, have a DAO class that does your data access functions.
>>
>>Here is a sample DAO class:
>
> [snip]
>
> Ahh.. I guess this is the same thing that Michael Sims suggested?
>
>
>>class CustomerDAO {
>>
>>
>>function getCustomer(..) {
>>...
>>//return a customer
>>}
>
>
> So I return a Customer object that has the set and get methods? And does
> that mean I do the following?
>
> ->set_first_name()
> ->set_last_name()
> ->set_address_1()
> ->set_address_2()
> ->set_address_3()
> ->set_city()
> ->set_state()

me, I have 'data object' that are subclasses of a 'peer' object, the
'peer' class has all the methods for data handling e.g.

Persistent::submit() (update and insert are handled/determined internally)
Persistent::get()
Persistent::find()
Persistent::findRange()
etc..

the 'data object's have definitions that stipulate 'field' objects
for each field in the database (usually a 1 to 1 relationship but not
always - for instance there is a VectorField for 1 to many stuff, and an
AssocField for many to many stuff).

if I have a Customer class I can do something _like_:

$cust = Persistent::get('Customer', array('CONTACT_ID' => $id));
$cust->firstname = 'Bob';
$cust->lastname = 'Builder';
$cust->submit();

the 'peer' class has __get() and __set() methods that find the requested
'field' object and return or set its value e.g. (very simplified)

class Persistent
{
function __get($name)
{
if (isset($this->fields[$name])) {
return $this->fields[$name]->getValue();
}

throw new Exception("field '$name' does not exist in this (".get_class($this).") object!");
}
}

maybe that gives you an idea about how to avoid constantly writing practically
the same getter/setter methods over and over... and also how to avoid
writing practically identical collection getter functions/methods
(how much different will you getCustomers() method be from your getProducts()
method ... in general anyway ... there are always exceptions to the rule! :)

> etc.
>
> Or is there a better way to handle it?
>
>
>>function getCustomers(..) {
>>...
>>// return a collection of customers
>>}
>
>
> How do I return a collection of customers?
>
>
>
> Thanks,
> Chris.
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация