You are here: Re: Objects and Arrays « PHP Programming Language « IT news, forums, messages
Re: Objects and Arrays

Posted by Jerry Stuckle on 04/03/07 13:48

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
>> instantiation 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("SELECT ...");
> $db2->query("SELECT ...");
> $rows[] = $db1->fetch();
> $more[] = $db2->fetch();
>
> which requires two (usually heavyweight) database objects, but instead do:
>
> $resultset1 = $db->query("SELECT ...");
> $resultset2 = $db->query("SELECT ...");
> $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("SELECT ...");
> $resultset2 = $db->query("SELECT ...");
> $rows[] = $db->fetchInto($resultset1);
> $more[] = $db->fetchInto($resultset2);
>
> 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@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация