Which solution/implementation is the best?
Posted: Wed May 12, 2010 8:20 am
Hi,
I have a little question, which might sound obvious to some of you, but having never "learned" programming/PHP other than by reading stuff (mostly on the Internet), I often find myself in such situation where I can't seem to be able to answer a (basic) question I have, nor find one online.
For instance, I am thinking of working on an app that would consist of a bunch of "commands" that would process user requests. Looking into doing some oriented-object programming, I envisioned each of those commands to be a class. Here was my first idea for some command, having two parameters :
But doing some more reading & thinking about it, I then came up with an alternative than is, I feel, more OO:
In the later, parameters are now dealt with using another class/object, and it seems to me a more OO approach to this, and possibly making this easier to later on work with/maintain, since everything params-related happens in that class. However, I'm not sure whether it really is better or not. Specifically, I'm wondering whether the former solution isn't more "optimized", as in will use less memory/time, since once the command is instantiated the params array exists/is available for use, whereas in the second one we need another class to be instantiated, and code to be executed, in order to get there, pretty much.
So I wonder which of those is really the best?
Trying to see how things could evolve in the future, I wondered about adding a "feature" so the param "id" would be renamed to "ident", but where the old/depreciated name "id" should still be supported. It's true that with the second solution it's all done inside class Params (but in the former it's all centralized in one place too, in function parse_params) and maybe it looks better.
For the former solution, it would be something like this:
And for the second solution, something like this:
This leaves me pretty much where I started, wondering which of those is better? Is the second one just a more OO approach to it, or are there really other advantages to it (e.g. later on, for maintenance and such) that I'm not quite seeing there?
Any help/insight will be appreciated.
Thanks,
-jacky
I have a little question, which might sound obvious to some of you, but having never "learned" programming/PHP other than by reading stuff (mostly on the Internet), I often find myself in such situation where I can't seem to be able to answer a (basic) question I have, nor find one online.
For instance, I am thinking of working on an app that would consist of a bunch of "commands" that would process user requests. Looking into doing some oriented-object programming, I envisioned each of those commands to be a class. Here was my first idea for some command, having two parameters :
Code: Select all
class cmdFoobar
{
protected $params = array('id' => 'int', 'label' => 'string');
public function execute($request)
{
$params = parse_params($this->params, $request);
}
}Code: Select all
class cmdFoobar
{
public function __construct()
{
$this->params = new Params();
$this->params->add('id', 'int');
$this->params->add('label', 'string');
}
public function execute($request)
{
$params = $this->params->parse($request);
}
}So I wonder which of those is really the best?
Trying to see how things could evolve in the future, I wondered about adding a "feature" so the param "id" would be renamed to "ident", but where the old/depreciated name "id" should still be supported. It's true that with the second solution it's all done inside class Params (but in the former it's all centralized in one place too, in function parse_params) and maybe it looks better.
For the former solution, it would be something like this:
Code: Select all
class cmdFoobar
{
protected $params = array('ident' => 'int', 'label' => 'string');
protected $deprec = array('id' => 'ident');
public function execute($request)
{
$params = parse_params($this->params, $request, $this->deprec);
}
}Code: Select all
class cmdFoobar
{
public function __construct()
{
$this->params = new Params();
$this->params->add('ident', 'int', 'id');
$this->params->add('label', 'string');
}
public function execute($request)
{
$params = $this->params->parse($request);
}
}Any help/insight will be appreciated.
Thanks,
-jacky