Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- objects vs arrays
February 5, 2010, 4:53 pm
rate this thread
I'm doing some private projects for home use like my own books
database or movies. For this projects like many others in web
programming typical tasks would be to grab books on some conditions
from db and show them in table. May be performing some calculation and
grouping or sorting.
At the moment, I do it with arrays and it is fine so far, but I asking
myself and you if it would be better to use objects? Obviously, Book
or Movie would be perfect candidate for an object. However it looks
like overhead for me to grab db result and create objects for each
book. Having assoc array I can easily sort on whatever property of
book, but how do I sort array of book objects?
Can someone give me some design tips for my scenario, please?
- Malcolm Dew-Jones
February 5, 2010, 4:42 pm
Re: objects vs arrays
Max (firstname.lastname@example.org) wrote:
: I'm doing some private projects for home use like my own books
: database or movies. For this projects like many others in web
: programming typical tasks would be to grab books on some conditions
: from db and show them in table. May be performing some calculation and
: grouping or sorting.
: At the moment, I do it with arrays and it is fine so far, but I asking
: myself and you if it would be better to use objects?
If it's a private project then it would be better to use objects _because_
that would be better for you - experience, skill, etc. Based on your own
experience you will then be better able to judge whether they are good or
bad for the next project.
: Obviously, Book
: or Movie would be perfect candidate for an object. However it looks
: like overhead for me to grab db result and create objects for each
As a note (and I think you agree, though I might misread you), if you use
objects in your program then _do not_ be tempted to serialize the objects
to save them as objects directly in a relational database. The database
should store data as rows with attributes as normal. Often several tables
of normallized data may be storing the data in the database for a single
object within the application.
Java programs appear to have lots of overhead creating objects from the
database rows/attributes, but Java programmers appear to have many
frameworks to automatically write the code that does that.
: Having assoc array I can easily sort on whatever property of
: book, but how do I sort array of book objects?
Haven't used it, but I believe that uksort will do this (also look at
uasort and usort). You define a function that compares two objects, and
that is used to sort them.
bool uksort ( array &$array , callback $cmp_function )
You could also extract the data pre-sorted from the database.
: Can someone give me some design tips for my scenario, please?
$0.02: To get started, don't use a relational database, simply dump the
serialized objects in a file. Load the file each time you start. That
way you spend your time on the objects and the interface to manipulate
them (and for a small project you might even just store the objects as the
"database"). If you use a relational database later you might be tempted
to just store the serialized objects - but don't. Instead add a layer to
convert between objects and normalized-data.
Re: objects vs arrays
On 5 Feb., 17:42, yf...@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote:
No, I don't do that.
Like Doctrine for PHP?
one more overhead :)
Yes, but sometimes one have more complex logic, so I try to understand
how OOP Gurus handle such situations
I've have already db with some data there.
Could you point to some good tutorial for creating such layer? I'll
try of course to google myself.
Thanks for your help!
- C. (http://symcbean.blogspot.c
February 5, 2010, 8:20 pm
Re: objects vs arrays
No, your thinking about the problem the wrong way. The methodology is
about how you address the problem - the specifics of the problem
should not determine the methodology. (that's not strictly true - but
explaining the difference would take several years of computer science
education - and space here is limited).
Certainly if you feel comfortable with the idea of learning OO around
the project you are proposing then by all means go ahead. But the
worst mistake to make would be to mix OO code with procedural code.
(Once again, this is not strictly true, but until you've mastered both
apporaches you are not in a position to know when to use which).
Exactly the same way. Why is it an overhead to store the data in an
object instead of an array element?
You're going to need much more objects than just a book. I'd recommend
writing down a description of how the system would be used - e.g.
user types in a word and clicks on search - she is presented with a
short list of books taken from a longer list stored in the database
where that word appears in the title or in the author's name.
Note that this just describes one way searching, you might describe
other scenarios e.g.
user types in two words and clicks on search - the lsit this produces
should contain a list of books where either word is in the title or
the author's name, but where both words match, these books are listed
Try to write in the present tense.
Once you're happy that you've described scenarios covering all the
behaviours of the system, go through your narrratives and underline
every noun and pro-noun.
You have then created a set of use-cases. The underlined items are
your objects. You have also defined a set of test cases to apply to