Hi! I have years of experience writing procedural code with PHP, and am finally learning OOP in PHP. But I have a question about the best method to interact with an instantiated object for those of you with lots of OOP experience.
Many of the tutorials I’ve watched 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.
Please let me know what you think, thanks!
Monty