# [EMAIL PROTECTED] / 2006-10-11 21:28:36 +0100:
> Richard Lynch wrote:
> >On Tue, October 10, 2006 6:14 pm, Chris de Vidal wrote:
> >>I want to create a "customer" class which fetches its attributes from
> >>a MySQL database.
> >
> >No, you don't. :-)
> >
> >This is a classic example of the "obvious" OOP solution being wildly
> >inappropriate.
> 
> Ok, so I now find myself in the unusual position of disagreeing with the 
> Lynchmeister. Why is this wildly inappropriate? IMHO this is what OOP is 
> all about.

    No, that's what trivial OOP examples applied to the letter where
    a different approach is in order are. If you study the GoF book [GoF]
    you'll see that the traditional claim "objects model physical
    entities from real world (and nothing else)" is very simplistic
    and terribly limiting.
    
    [GoF] http://www.amazon.com/dp/0201633612/

    If you paint yourself into this corner you'll find your code
    grinding the database to death in the OO-relational impedance
    mismatch. The OOP ideal of a single source of data is nice,
    unfortunately the real world gets in the way.
 
> You can encapsulate everything to do with a customer in an object. This 
> ensures that you don't duplicate any code that works on customer data. 
> You avoid duplication of code. As a result you can ensure data integrity 
> because there is only one route to read and write it.
 
    Who talks about duplicating business logic? You just need to have
    more than one access point for the data.

> If this is not what you think OOP is all about, do please enlighten us 
> as to the error of our ways.

    Imagine deleting many rows in a table one by one (pseudocode):

    class Record
    {
        function __construct($id)
        {
            $this->id = $id;
        }
        function delete()
        {
            // DELETE FROM some_table WHERE id = $this->id
        }
    }
    foreach ($records as $r) {
        $r->delete();
    }

    instead of taking them with a single DELETE:

    // assuming
    // class Search;
    // $searchBuilder = new SearchBuilder;
    // $SearchBuilder->add(new LessThan('id', '5000001'));
    // $search = $searchBuilder->result();

    class RecordSet
    {
        function __construct(Search $search)
        {
            $this->search = $search;
        }
        function delete()
        {
            // DELETE FROM some_table WHERE $this->search->toSQL()
        }
    }

    $recordset($search);
    $recordset->delete();

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to