Which solution/implementation is the best?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jacky
Forum Newbie
Posts: 6
Joined: Tue Jul 14, 2009 5:27 am

Which solution/implementation is the best?

Post by jacky »

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 :

Code: Select all

class cmdFoobar
{
	protected $params = array('id' => 'int', 'label' => 'string');
	public function execute($request)
	{
		$params = parse_params($this->params, $request);
	}
}
But doing some more reading & thinking about it, I then came up with an alternative than is, I feel, more OO:

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);
	}
}
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:

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);
	}
}
And for the second solution, something like this:

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);
	}
}
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
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Which solution/implementation is the best?

Post by PHPHorizons »

Neither one looks more object oriented than other to me. It's hard to know right now what will be better long term.

The consensus seems to center on just getting the job done first, and only when bottlenecks appear do you do any optimizations. In other words, the process of premature optimization is actually a bottleneck of it's own. The best solution is to simply forge ahead with either of those paradigms. If you find that the choice you made doesn't meet the standards that it needs to, then you can do some refactoring.

I do have one recommendation though concerning the code at hand. If the parse method works the same for all commands, then that parse function should be defined only once. If it works differently for each command, then it should probably be defined in each of the command classes, perhaps with an abstract parse as a foundation.

I hope that helps.

PS, one thing I forgot to mention, the parse_params function does seem to be incorrect. If params are being defined in the class, the class should not be dependant on a function that's not part of any class to parse the params. That parse_params function just seems to be flying out in space somewhere on it's own. $this->parse_params would be fine, especially if it's based on some abstract or parent class method.

Cheers
Post Reply