This is not really a problem but more of a question.
My question is how it’s ‘supposed to be’ programmed.
It might not be too clear to explain, though.
So my question is; do I have to make multiple methods to retrieve data from a database?
For example, I have a table called FRUITS.
It contains the ID, name and date the fruit was added.
Now I want to get the name of the fruit based on a given ID, and later on in the script I want to get the date of the fruit as well.
Should I make one method such as get_fruit($id) which returns both the name and date, or two separate methods get_name($id) and get_date($id)?
Thanks in advance.
You should use one object which would contain all the required data. For example:
Also implementing
__setand__getwould be nice example of using full php potential.You also may implement
save()method (or extend database row class, such asZend_Db_Table_Row.So whole code would look like:
EDIT: using separate methods to load certain data is useful (only?) when you need to load large amount of data (such as long texts) which is required only on one place in your code and would affect performance.
EDIT 2: (answer to comment):
__getand__setare called when you try to access undefined property of an object, for example:There are two “large” approaches to this that I know about:
To sum up… First approach is easy to implement, is fully automatized… You don’t need large amount of code and sources would be pretty much the same for every class. The second approach required more coding, but gives you a better control. For example:
But on the other hand, you can do this with first approach:
Or you can use total combination and check for getter/setter first and than try to access property directly…