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