Hash tables vs Switches
Moderator: General Moderators
Hash tables vs Switches
Anybody have any comments on the idea of using hash tables in the place of switches? I've heared talk of this idea at various times, but never really jumped into it. What are you opinions on it's use in PHP?
Cheers
Cheers
Re: Hash tables vs Switches
it depends on what do you mean exactly. Is it something like this:BDKR wrote:Anybody have any comments on the idea of using hash tables in the place of switches?
Code: Select all
$switch = array(
'some_action' => 'some_action_func',
'some_other_action' => 'some_other_func',
);
if( isset($_GET['action']) && isset($switch[$_GET['action']]) ) {
$func = $switch[$_GET['action']];
unset($_GET['action']);
call_user_func_array($func, $_GET);
}
function some_action_func() {
$args = func_get_args();
// do something
}
function some_other_func() {
$args = func_get_args();
// do something else
}No, that's not what I had in mind. I was thinking something along the lines of...
... as opposed to ...
I really don't think too much of this as I really don't see it much being used in PHP. Perhaps in the PHP-GTK crowd where an execution loop of sorts may be run, in a web application there would only be one run through the script so any possible gains would most likely be extremely negligible.
Just messin' around.
Code: Select all
$hash_table=array
(
'one' => 'echo_one',
'two' => 'echo_two',
'three' => 'echo_three',
);
$action=&$hash_table;
function echo_one()
{ echo '1'; }
function echo_two()
{ echo '2'; }
function echo_three()
{ echo '3'; }
$action[$var]();Code: Select all
$action = 'two'; /* This could be a var coming from a form */
switch ($action)
{
case 'one':
{ echo_one(); break; }
case 'two':
{ echo_two(); break; }
case 'three':
{ echo_three(); break; }
}Just messin' around.
Ooops!Weirdan wrote: Actually I don't see any substantial difference between your example and mine![]()
Code: Select all
$func = $switch[$_GET['action']];
unset($_GET['action']);
call_user_func_array($func, $_GET);Code: Select all
$func=&$switch[$_GET['action']];
$switch[$func]($_GET);Sorry about the that.
But anyway, I was really just thinking about it. I doubt there would be much (if any) in the way of a serious speed increase with this method. In spite of the fact I've been called a code smith (with derogatory intent) this wasn't my reason for asking.
Instead, I'm thinking about a design/orginaztion approach that would make sense in code where there is an execution loop of sorts with many possible logic paths. As opposed to using some gigantic switch, I am wondering if this is an option that could improve readibility. Such an thing wouldn't be that likely in a web app but maybe in a PHP-GTK application.
One drawback tho is the question of vary agument requirement for the different functions / object->methods().
But like I said, I am just kinda thinking out loud.
Cheers
All this work because the language is not sufficiently expressive. Come to the python side, BDKR:
Code: Select all
actions = {'alpha': do_alpha, 'beta': do_beta}
actions.get(funcname, print_error)()
def do_alpha():
#do something
def do_beta():
#do the other thing
def print_error():
print "encountered an error"In cheesiesf Luke Skywalker voice, "Twigletmac! Why didn't you tell me llimlib is my father?"llimllib wrote:All this work because the language is not sufficiently expressive. Come to the python side, BDKR:
Code: Select all
actions = {'alpha': do_alpha, 'beta': do_beta} actions.get(funcname, print_error)() def do_alpha(): #do something def do_beta(): #do the other thing def print_error(): print "encountered an error"
Seriously, you don't have to tell me about Python. It's an awesome language. After spending some time where I'm at now, I'm planning on doing some wxPython stuff. Or at least that's the plan.
Cheers