How do I use objects?
I know so little I'm even having trouble asking this question...
I think I get the basic idea of objects. Inheritance, polymorphism and so on, but I'm having a really hard time with understanding how to model (is that even the right term?) objects for a project.
I'm creating a page that displays results from a database into an HTML table. I create a new HTML row directly from the mysqli result object, like so (in PHP):
while($result = mysqli_fetch_assoc($query_result)){
echo "<td> $result['NAME'] </td>";
echo "<td> $result['COMPANY'] </td>";
echo "<td> $result['ID'] </td>";
...
...
}
Should I be creating a Result object from the results of the query and then using a method to display them? Should I be using a Table object to deal with the HTML code?
After the user selects one of the results, I want them to answer a series of questions. Should I be using a Questionnaire object to hold them and then a method to write them to my database, or can a simple associative array do the job (this is what I'm using right now)?
I only really confortable with functional programming so this whole object oriented style is very confusing. There are so many possibilities at every step that it's kind of overwhelming. When do I use an object for something?
Any help you could give would be very much appreciated.
Posted Jan 15, 2020, 8:26 PM UTC
I don't see any need for objects in that snippet of code.
Remember, objects are just a tool. They don't help the computer at all, they're just there for us humans to make it easier to organize larger programs.
In that sense, it's just like functions - you can write a program without any functions, but it's usually a lot more concise and readable when you use functions rather than duplicating code.
Beginning programmers tend to overuse object-oriented programming, especially inheritance. The reality is that small programs don't need any OO at all. Medium programs can sometimes benefit from using some classes/objects just for modularity and that's it.
But for large programs, OO can be fantastic.
Here's an example: let's say you had 3 pages on your website, that all created a table based on a database. All of the tables are similar, but they read slightly different columns from the database. Now you've got a great excuse to define a class.
You could make a class that's constructed based on the columns, then you just need to create a different object - a different instance of that same class - on each of your three pages.
Does that make sense?
It does make sense, thank you.
I actually do have a couple of pages with tables, just like you mentioned, but each table needs something different from the others.
Some need this type of id, some that, some need a hidden column for sorting, etc. I ended up creating functions for the parts that are the same and just manually coding the parts that are different.
How would I translate that into objects? Have a GenericTable object that CustomerTable and EquipmentTable inherit from? From your comment I get the feeling that inheritance isn't a good thing, so maybe each table would be composed of smaller objects, like TableHead, TableBody and things like that?
You want one class - something like MyTable. The class has a constructor and other methods that operate on the table.
Then, each time you need a specific instance of a table on the page, you create an instance of the table. So CustomerTable is an object - a variable. It makes use of the same class, but it initializes it differently and/or calls different methods on it, so it behaves differently.
Smaller objects inside like TableHead, TableBody is way overkill. Don't add more objects for no reason.
You have a great argument for creating one class, and three instances of that class (three objects). Start with that, don't add more complexity than is warranted.
Thank you, I'll try to implement the objects like you suggested.