Objects and Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Stuckle

    #16
    Re: Objects and Arrays

    Toby A Inkster wrote:
    Jerry Stuckle wrote:
    >Simon Stienen wrote:
    >>On 2007-04-03 04-52-47, Jerry Stuckle wrote:
    >>>
    >>>Not at all a bad design. Any class will do the same thing. It
    >>>handles a single query. If you make another query, that will
    >>>overwrite the original query. If you want two concurrent queries,
    >>>you use two db objects.
    >>One query per _DATABASE_? Sorry, but I think this is a somewhat
    >>limited approach.
    >No, the DB object represents a QUERY. You can have one QUERY open
    >at a time. This is pretty standard for objects - each
    >instantiatio n handles a specific object.
    >
    The normal way that database objects seem to operate in PHP is that one
    database object can handle multiple concurrent queries by returning a
    "ResultSet" type element for each query, such that the ResultSet can be
    assigned to a variable and used at a later date, and the main database
    object can be used for other queries in the meantime.
    >
    That is, they do not do:
    >
    $db1->query("SELEC T ...");
    $db2->query("SELEC T ...");
    $rows[] = $db1->fetch();
    $more[] = $db2->fetch();
    >
    which requires two (usually heavyweight) database objects, but instead do:
    >
    $resultset1 = $db->query("SELEC T ...");
    $resultset2 = $db->query("SELEC T ...");
    $rows[] = $resultset1->fetch();
    $more[] = $resultset2->fetch();
    >
    which only requires one database object.
    >
    Examples of the kind of behaviour Simon's describing include the mysqli
    extension's "query" method, PDO's "query" method, and PEAR package DB. The
    same principles, though using a slightly odd syntax can be found in PEAR's
    MDB package:
    >
    $resultset1 = $db->query("SELEC T ...");
    $resultset2 = $db->query("SELEC T ...");
    $rows[] = $db->fetchInto($res ultset1);
    $more[] = $db->fetchInto($res ultset2);
    >
    Again the same principle can be seen in most of the older non-OO database
    interfaces, though (obviously) using a less object-oriented syntax:
    >
    $resultset1 = dbx_query($db, "SELECT ...");
    $resultset2 = dbx_query($db, "SELECT ...");
    $rows[] = dbx_fetch_row($ resultset1);
    $more[] = dbx_fetch_row($ resultset2);
    >
    Sure, Toby, that's one way to do it. But it's not the only way and not
    always the best.

    I think part of the confusion here is the name $db. The way he has it
    coded it isn't the database or connection itself - it's more like an
    object representing some generic data in the database, with the query
    being a request to the database.

    I use similar objects all the time as business objects. For instance, I
    might have an Employee object, where I can hold a single employee. Some
    of the methods in this might be "get", "add", "delete" and "update".

    Another object would be an EmployeeList, which (obviously) gets a list
    of employees. I might, for instance, be able to fetch a list of
    employees by department or manager.

    This way is just a more generic extension of the latter. I personally
    wouldn't do it this way generically, but it's a perfectly valid OO design.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    Working...