Reputation: 304
Many of the tutorials I've watched about PHP OOP use getter and setter methods to set invidivual properties and retrieve data from an object. But I'm wondering if the second approach of passing all properties in an array is okay and if not, why?
$obj = new MyClass();
OPTION 1: Setter & Getter
$obj->setKeyword("orange");
$obj->setTitle("Title Name");
$obj->setPage(4);
$obj->setContent("Blah blah blah");
$obj->setTemplate("template.tpl");
$disp = $obj->getContent();
OPTION 2: Array & Getter
$params = array('keyword' => "orange",
'title' => "Title Name",
'page' => 4,
'content' => "Blah blah blah",
'template' => "template.tpl"
);
$obj->setParams($params);
$disp = $obj->getContent();
I'm leaning toward OPTION 2 for a script I'm writing because there are about two dozen properties that can potentially be set for the object, and I feel that maybe just sending them all in an array is the easiest way to do it. I also don't need to create a get and set method for every property, which I think would cut down on the amount of code I need to create for the class.
Is there anything wrong with Option 2? Is it better to use getter and setter methods as shown in Option 1? Thanks!
Upvotes: 1
Views: 190
Reputation: 1084
Individual gets and sets give you an opportunity to manipulate the data on the way in and out, or run some other kind of logic. That's usually the driver for whether I have them or not. If the object is purely storage nothing wrong with mass set, but consider the future possibilities as well... I've seen these kind of things degenerate when someone comes along to make a quick fix and does something like:
public function setParams($params){
$params['x']=$params['x']*2;
etc...
}
Upvotes: 1
Reputation: 1509
There is nothing wrong with either one. Both are just fine. Just depends on how you like to implement it. But if you like the 1st approach and don't want to write a new method for every property. Well, if the number of properties is as big as two dozens, you don't need to write one method for every individual property. Instead you can use __get() & __set() magic methods. Something like this:
class MyClass
{
private $publicProperties = ['abc'=>NULL,,'xyz'=>NULL]; //add all properties you want to be accessible. with their default values.
public function __get($name)
{
if(array_key_exists($name, $this->publicProperties))
return $this->publicProperties[$name];
throw new Exception('properties Does Not Exist.');
}
public function __set($name, $value)
{
if(array_key_exists($name))
{
$this->publicProperties[$name] = $value;
return true;
}
throw new Exception('Proeperty Does Not Exist.');
}
}
Now you can set the properties like following:
$class = new MyClass;
$class->abc = 'New Value';
And then access it like this:
echo $class->abc; //output should be: New Value
If you like to set the value using a method, like $class->abc('New Value')
instead of $class->abc = 'new value'
, you can use __call($name, $value)
instead of __set($name, $value)
Upvotes: 0
Reputation: 2222
There is nothing wrong with your second approach.
Both must be available in my opinion.
Almost every collection implementation that I know offers at least one mass assignment method.
Upvotes: 5