Re: Suggestions for class design

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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:


class CustomerDAO {

function getCustomer(..) {
...
//return a customer
}

function getCustomers(..) {
...
// return a collection of customers
}

function saveCustomer(..) {
...
}
 
}


Hope this helps.
--
Anas Mughal



On 9/19/05, Chris W. Parker <cparker@xxxxxxxxxxxx> wrote:
> 
> Hello,
> 
> Every time I write some classes I always come across the same
> fundamental problem that I haven't figured out how to solve/approach
> yet. I'm not exactly sure how to put it into words simply but here
> goes...
> 
> Let's take for example a class called 'Customer' that (obviously)
> manipulates customers in the database. Here is a very basic Customer
> class. (Data validation and the like are left out for brevity.)
> 
> class Customer
> {
> var $id;
> var $name;
> 
> function add_customer()
> {
> // create sql statement
> $sql = "INSERT INTO ...";
> 
> // create db connection
> $db =& DB::singleton();
> 
> // execute SQL
> $db->execute($sql);
> }
> 
> function delete_customer()
> {
> // create sql statement
> $sql = "DELETE FROM ...";
> 
> // create db connection
> $db =& DB::singleton();
> 
> // execute SQL
> $db->execute($sql);
> }
> 
> function get_customer()
> {
> // create sql statement
> $sql = "SELECT ... FROM ...";
> 
> // create db connection
> $db =& DB::singleton();
> 
> // execute SQL
> $customer = $db->execute($sql);
> 
> // populate object variables
> $this->id = $customer['id'];
> $this->name = $customer['name'];
> }
> }
> 
> (Unless I've already got some major design flaws I think we should be
> good to go.)
> 
> Where I get tripped up is when I realize I'll need to at some point get
> more than one customer at a time and thus I want to add a method called
> 'get_customers()'.
> 
> Since an object should be a single instance of something (e.g. ONE
> customer) how do I justify adding the method 'get_customers()'? Or
> better yer, how do I properly add a method like that? (A method where
> instead of using a SQL statement to return ONE customer's data I instead
> return a record set with more than one customer.)
> 
> The class in its current state can't handle data like that so I have to
> then rethink the design and a new object variable called $customers
> where I store the record set that is in return accessed directly by the
> calling page itself.
> 
> With the way I'm working with these objects now they seem less like
> objects and more like an organized collection of functions.
> 
> 
> I'd appreciate some actual code recommendations as I have a hard time
> understanding the abstract descriptions of how objects should work and
> be designed.
> 
> 
> 
> Thanks,
> Chris.
> 
> Thanks!
> Chris.
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 


-- 
Anas Mughal

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux