[PHP] Is it better to return a multi-array, or an array of objects?

2006-03-03 Thread Daevid Vincent
I'm building a fairly large project. I've been trying to follow best
practices, but I'm a bit worried about performance. Using PHP 5.1 and mySQL
5.0.

I have product and company classes.

So when a user does a search of the database, is it better/faster/etc. to
return just a two dimensional associative array to populate an HTML table of
the results like: product name, product id, price, company name, company
phone, etc. (assume I'm only showing some of the total number of fields).

Or, should my SQL method return me an array of product objects, which then
inturn contain a company object? Keeping in mind, these objects are then a
complete snapshot of the database row they represent, along with other
fields and such that are created upon instantiation.

Does this make sense what I'm asking?

Are these things negligible?

Does it matter that I'm only showing say 10 fields of perhaps 50 or more
possible ones (that the classes and database would hold)?

My gut feeling is to use an array as it seems less overhead and faster.
However the object versions seems to be more expandable and maintainable
perhaps. Although a bit more work due to all the method calls to pull out
the stuff to display in the HTML.

I could run timing tests, etc, but I only have like 10 products right now in
the database while I'm building, and without REAL world data, I think my
tests will be skewed.

Lets say that I expect several thousands to a hundred thousand or so people
searching the database via the web UI when this is finally completed.

ÐÆ5ÏÐ 

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



Re: [PHP] Is it better to return a multi-array, or an array of objects?

2006-03-03 Thread chris smith
On 3/4/06, Daevid Vincent [EMAIL PROTECTED] wrote:
 I'm building a fairly large project. I've been trying to follow best
 practices, but I'm a bit worried about performance. Using PHP 5.1 and mySQL
 5.0.

 I have product and company classes.

 So when a user does a search of the database, is it better/faster/etc. to
 return just a two dimensional associative array to populate an HTML table of
 the results like: product name, product id, price, company name, company
 phone, etc. (assume I'm only showing some of the total number of fields).

 Or, should my SQL method return me an array of product objects, which then
 inturn contain a company object? Keeping in mind, these objects are then a
 complete snapshot of the database row they represent, along with other
 fields and such that are created upon instantiation.

 Does this make sense what I'm asking?

 Are these things negligible?

 Does it matter that I'm only showing say 10 fields of perhaps 50 or more
 possible ones (that the classes and database would hold)?

 My gut feeling is to use an array as it seems less overhead and faster.
 However the object versions seems to be more expandable and maintainable
 perhaps. Although a bit more work due to all the method calls to pull out
 the stuff to display in the HTML.

 I could run timing tests, etc, but I only have like 10 products right now in
 the database while I'm building, and without REAL world data, I think my
 tests will be skewed.

 Lets say that I expect several thousands to a hundred thousand or so people
 searching the database via the web UI when this is finally completed.

I don't think you'll find much difference either way.. I wouldn't
return a whole object though - lots of memory usage there for a small
result set.

If you return an array of data, you have to firstly turn the results
into an array, then after you return it you have to go through the
array again to display the results.. so you're doubling up a little.

Then again - it depends on how your app is set up. Personally I use my
own API to fetch data so it returns an array, then the front can do
whatever with it (display html, display xml - doesn't matter to the
API).

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



Re: [PHP] Is it better to return a multi-array, or an array of objects?

2006-03-03 Thread Murray @ PlanetThoughtful

On 4/03/2006 3:10 PM, Daevid Vincent wrote:

I'm building a fairly large project. I've been trying to follow best
practices, but I'm a bit worried about performance. Using PHP 5.1 and mySQL
5.0.

I have product and company classes.

So when a user does a search of the database, is it better/faster/etc. to
return just a two dimensional associative array to populate an HTML table of
the results like: product name, product id, price, company name, company
phone, etc. (assume I'm only showing some of the total number of fields).
  

[snip]

One observation: you shouldn't return all fields in a recordset unless 
you *need* all of the fields in a recordset.


The majority of the time, you should be explicitly stating which fields 
to retrieve in your SQL statement, as in:


SELECT field1, field2, field8 FROM mytable WHERE whatever

as opposed to:

SELECT * FROM mytable WHERE whatever

Some of the time, yes, that will mean you're explicitly typing out 7 
field names from a table that only has 8 field names, but getting out of 
the habit of using *, particularly if you expect your application to 
work well under heavy loads, will save you resources, which will 
increase your app's overall performance.


It may be you're already doing this, but it wasn't clear from your post.

Much warmth,

planetthoughtful
---
Lost in thought
http://www.planetthoughtful.org

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



RE: [PHP] Is it better to return a multi-array, or an array of objects?

2006-03-03 Thread Daevid Vincent
 SELECT field1, field2, field8 FROM mytable WHERE whatever
 
 as opposed to:
 
 SELECT * FROM mytable WHERE whatever

 It may be you're already doing this, but it wasn't clear from your post.

Yes, Murray, thank you, and I am already doing this. :)

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