Yo! I have some questions about different approaches and best practices using OOP.
Methods and arguments
I want to update different columns in my person table, and I wonder which methods and arguments I should use.
Approach 1:
A generic method that can handle all column updates
$personTable->save($person);
Approach 2:
A specific method for each kind of update
$personTable->saveName($person->name);
$personTable->saveLocation($person->location);
$personTable->saveNameAndLocation($person->name, $person->location);
I was thinking about combining both approaches, using approach 1 whenever I have 2 or more columns to update, but using appraoch 2 when I only have 1 column to update.
I guess it might be hard to decide without knowing how many times these methods will be called, so letâs assume this:
- saveName() called twice or more
- saveLocation() called twice
- saveNameAndLocation() called once
If I choose to use appraoch 2 at all, is it really necessary to have both saveName($name) and saveLocation($location)? I could just go with saveColumn($value, $column) and use the $column argument to choose the column. I think this makes the code a lil bit dirty, but Iâm not sure if itâs really bad to do.
Method names
Inside my PersonTable class I currently have getById($id), getByName($name) and create($name).
The reason itâs called create($name) and not createByName($name) is because I never create the person in any other way, unlike the get methods that needs the âbyIdâ and âbyNameâ suffix. If I ever want to add another way to create a person Iâll add an suffix, but maybe it would be best to just use the suffix from the very start, it might be a bit easier to maintain and it will be more consistent, but I feel like itâs a bit ugly xD
Edit: Do you think itâs better to name the methods savePerson() instead of just save()? I do $personTable->save() and I think thatâs cleaner and obvious enough.
I had some more questions, but I forgot them, and this is already long enough as it is. Thanks for reading. ^~^