OO: General Question

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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

OO: General Question

Post by raghavan20 »

Assume I have a 'Customers' table and a class which stores information on customers.

Customers Table:
Id integer(10) auto_increment,
FirstName varchar(30),
LastName varchar(30),
Street varchar(30),
City varchar(30),
Country varchar(30),

Code: Select all

class Customers{
var firstName;
var lastName;
var street;
var city;
var country;

function Customers(){}
function getFirstName(){}
function getLastName(){}
function getStreet(){}
..function getCountry(){}

}
1. I want to display the customer details with the first name, 'John'
2. I want to display all the customers' details
3. I want to create a new customer, how do I persist into mysql using OO?
4. How do I alter the address of a customer, say suppose 'Mark'

How do I build the functions?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I think the naming is a bit odd.. You represent the Customers, and then you have a method named getFirstName. I would think each Customer has a getFirstName.


Anway, in my implementation of an ActiveRecord i would probably write the functions as following:

All the customers with first name = 'John':

Code: Select all

$customers_with_john = $customers->get('first_name=?', array('John'));
All the customers:

Code: Select all

$customers_all = $customers->get();
Create a customer:

Code: Select all

$customer = array('first_name' => 'John', 'last_name' => 'Doe');
$customers->add($customer);

Code: Select all

$customer = $customers->get('first_name=?', array('Mark'));
$customer['address'] = 'A new streetname';
$customers->update($customer);
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

If you dont mind, can you define your get() member function. I am not able to understand your solution without the definition of methods and how you are going to access the db in the methods.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

ActiveRecord methods:

add($row) // an associative array attribute=value
remove($row) // an associative array attribute=value, only primary key is required
update($row) // an associative array attribute=value, primary key is used for WHERE, other attributes for SET
get($where, $parameters) // a prepared statement and it's parameters
count($where, $parameters) // a prepared statement and it's paramters

And then some functions that have influence on the backend sql, but apart from the orderby and limit they shouldn't be used. (setters and getters for select, from, where, groupby, having, orderby, limit clauses).

----------------------------------------------------------------------------------

Now, if you have a Customer and Customers class, you could easily use composition to achieve some storage:

Code: Select all

class Customer
{   
   // customer attributes
   var $firstname; 
   var $lastname;

   function getAttributes()
   {
         return array('firstname', 'lastname');
    }

   function setFirstName($firstname)
   {
      $this->firstname = $firstname;
   }

   function getFirstName()
   {
       return $this->firstname;
   }
}

class Customers
{
   // backend
   var $activerecord;

   function addCustomer($customer)
   {
      $row = array();
      $attributes = $customer->getAttributes();
      foreach($attributes as $attribute)
      {
         $row[$attribute] = $customer->get{$attribute}();
      }
      $activerecord->add($row);
   }
}
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

timvw wrote:ActiveRecord methods:
Strictly speaking that's a RowDataGateway. ActiveRecord is a domain object. It looks pretty similar - it has methods to do its own data access - but the key point is that it also carries out some domain logic.

rhagavan: all the options for data access classes and much more are explained very well in Martin Fowler's Patterns of Enterprise Application Architecture - the pattern catalogue is online here but it doesn't go into much detail compared to the book. It's a must-buy and you won't be the same programmer once you've read it.

There's also a good article about data access stuff here at phppatterns.
Last edited by McGruff on Sat Aug 06, 2005 5:29 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I agree that the ActiveRecord looks like a TableDataGateway, but it's hard to actually implement domain logic in an abstract class :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

The two often seem to get mixed up so I thought it was worth pointing out wthout meaning to detract from your helpful post.

In case anybody's wondering what we're talking about, domain logic basically just adds some kind of value to raw data. For example, you might greet members with a happy birthday message on their birthday. An ActiveRecord would have the "is today the member's birthday?" logic and an isBirthday() accessor. It's a useful pattern for php web apps.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I dont want to fake I understand everything you guys were talking of. I understand most of them but since I am developing the first OO application which makes use of database I want ideas on general classes that have to be created.

like:
database wrapper class and do we have to create classes for
1. input - textbox
2. input - textarea and so on

I jus want ideas on what are the basic and necessary classes an OO application should have. I just want to create a few classes which I should be able through out all the OO applications I will make in the future.

I would appreciate if you can me the necessary list of classes to be made and the code if you have.
There's also a good article about data access stuff here at phppatterns
The link at PHPPatterns is broken.

Thanks for your help guys:-)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

phppatterns seems to be down for the moment, but if you try later on this week, it should work though ;))

Here is an example of you how can use PEAR classes to do what you are trying to do http://www.21st.de/downloads/rapidprototyping.pdf
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

raghavan20 wrote:I would appreciate if you can give me the necessary list of classes to be made and the code if you have.
Extreme Programming (XP) shuns "big upfront design" in favour of evolutionary development in little steps, all the while tightly focussed on client requirements. The answer to your question in XP terms would be what are the requirements for the app you're working on? These are turned into classes using test-driven-design where you write some (failing) tests then write just enough code to make the tests pass. As you write one class, you often discover neighbouring classes: these are mocked out temporarily and implemented later.
Post Reply