When is the right time to use functions to set variables inside of my models, verse simply directly assigning them?
AKA...
When do I use:
$model->set('status','active');
...instead of:
$model->status = 'active';
When is the right time to use functions to set variables inside of my models, verse simply directly assigning them?
AKA...
When do I use:
$model->set('status','active');
...instead of:
$model->status = 'active';
using getters and setters is good for the encapsulation of code. It's always a good idea to use them.
Think about this:
If you're using a recent version of PHP (5.x), you can get the best of both worlds with the magic methods __get()
and __set()
. (PHP reference). Any access to an undefined property will be routed to the appropriate get/set function automatically.
I must admin that I'm not certain if this is really a good idea to use or not, but it seems like a decent fit for a base model class in a MVC system.