...

View Full Version : Extending the switch statement



aditron
03-12-2009, 03:49 AM
Hi,

I've written an online app that has a ton of switch statements depending on which installation type I use for the app itself, and I'm trying to streamline the switch statements themselves.

Does anyone know if it is possible to do something like this?

menu.php


switch ($_GET["action"]) {

include("test.php");

}

and then import the case statements in the test.php file
eg.



case "tom":
include("tompage.php");
break;


I haven't gotten it to work just yet, but I'm sure there's a way to... Anyone have any ideas?

oesxyl
03-12-2009, 05:52 AM
I would prefere to use a hash array instead:

menu.php:


$mapext = array('tom' => 'tompage.php');




include('menu.php');
$ext = $_GET["action"];
// some code here to be sure that $ext is safe
include($mapext[$ext]);


best regards

milanchheda
03-12-2009, 05:57 AM
i dont think thats a good idea.
May be you can do something like, include the file, say functions.php, which would have functions for each of your cases.eg
function tom();
function xyz();
etc...

and on each case call its respective function.
Inclusion of each file in the switch case in not a good programming and also it can effect the performance and the efficiency of the application.

If toppage.php is an external file which should be loaded when the case "tom" is selected then may be you use header() to redirect it to the tompage.php

oesxyl
03-12-2009, 06:08 AM
i dont think thats a good idea.
May be you can do something like, include the file, say functions.php, which would have functions for each of your cases.eg
function tom();
function xyz();
etc...
not a good idea too, :) this will add a function call and a lot of code to write functions. :)


and on each case call its respective function.
Inclusion of each file in the switch case in not a good programming and also it can effect the performance and the efficiency of the application.

If toppage.php is an external file which should be loaded when the case "tom" is selected then may be you use header() to redirect it to the tompage.php
the best way to implement this, in my opinion, is to use oop and using inheritance and polymorphism to get rid of switch and reuse a lot of code. Anyway I don't think that op want to rewrite the code, so this is a little off topic, :)

best regards

milanchheda
03-12-2009, 06:14 AM
not a good idea too, :) this will add a function call and a lot of code to write functions. :)


the best way to implement this, in my opinion, is to use oop and using inheritance and polymorphism to get rid of switch and reuse a lot of code. Anyway I don't think that op want to rewrite the code, so this is a little off topic, :)

best regards

Ya, i agree with you on that.
And i didnt wanted to tell that your idea was a bad one.
I was pointing to the aditron's idea.
It just happened that we both posted the replies at the same time and my reply came after you.

oesxyl
03-12-2009, 06:23 AM
Ya, i agree with you on that.
And i didnt wanted to tell that your idea was a bad one.
I was pointing to the aditron's idea.
but is bad, :) as bad as the op idea, :) ( or maybe worst, I don't know)


It just happened that we both posted the replies at the same time and my reply came after you.
it's no problem with that, :)
is better for everybody if more then one user help in a thread.

best regards

aditron
03-13-2009, 12:52 AM
Thanks oesxyl,

I'll give that a shot - there shouldn't be too much rewriting to implement your idea, and if it does what I want, then the benefits will be totally worthwhile. I'll post back with my level of success.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum