Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Extending the switch statement

    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

    PHP Code:
    switch ($_GET["action"]) {

    include(
    "test.php");


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

    PHP Code:
      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?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    I would prefere to use a hash array instead:

    menu.php:
    PHP Code:
    $mapext = array('tom' => 'tompage.php'); 
    PHP Code:
    include('menu.php');
    $ext $_GET["action"];
    // some code here to be sure that $ext is safe
    include($mapext[$ext]); 
    best regards

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by milanchheda View Post
    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

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by oesxyl View Post
    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.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by milanchheda View Post
    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

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •