Currently, I build two classes for each database table. For instance, if I have the table person, I will have the classes Person_List and Person.
Design-wise, is it better
- for
Person_Listto output an array ofPerson; or - for it to output an array containing arrays of rows in the table.
Performance-wise, which is better?
I believe that design-wise, and taking performance into account, would be to (if you insist on
Person_Listclass to represent table andPersonto represent single record):Iteratorinterface forPerson_Listclass, so you can iterate through the table without the need to pull all the records at once (it should be significant performance gain in some cases),Countableinterface forPerson_Listclass, so you are able to count all the results if necessary by getting count directly from database,This should give you flexibility and allow you to use
Person_Listclass objects similarly as arrays.If you still have problems employing these two interfaces, here is some explanation:
foreach ($table as $record)(where$tableis an instance ofPerson_List), thecurrent()method ofPerson_Listclass will be invoked (because it is a part ofIteratorinterface – see docs here), which should return an object ofPersonclass; this should happen using eg.mysql_fetch_object();count($table)(where$tableis an instance ofPerson_List), thecount()method ofPerson_Listclass will be invoked, which in turn can use eg.mysql_num_rows()function to return all the results instead of pulling them from database and then counting (this will be again significant performance gain),