Re: Suggestions for class design
| From: | Jochem Maas | Date: | Tue, 20 Sep 2005 08:44:25 +0000 |
| Subject: | Re: Suggestions for class design | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-222736@lists.php.net to get a copy of this message | ||
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>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! :)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()
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.