-2

I have this model class that has several functions in it. I need to get the variable $new_route after its been set by the if statements to be available to all the other functions. I included them initially at the top of the class, not in a function, but the if statements kill that. I can include those lines of code in every function and it works the way I want, but I know that way sucks. I wrapped the code in a function getRoute() but how do I execute the function inside the class automatically so that the variables are available for all the other functions?

  public function getRoute() {
                 $find_route = $_SERVER['QUERY_STRING'];
             $get_route = explode('&',$find_route);
            $set_route = explode('/',$get_route[0]);
             $new_route = $set_route[1];    
                if ($new_route == 'memberships'){
                    $new_route = "MEMBER";  
                }elseif ($new_route == 'donations'){
                    $new_route = "DONATE";
                }elseif ($new_route == 'tickets'){
                    $new_route = "TICKET";
                }elseif ($new_route == 'items'){
                    $new_route = "ITEM";
                }elseif ($new_route == 'peer'){
                    $new_route = "PEER";
                }elseif ($new_route == 'recurring'){
                    $new_route = "RECURRING";
                }
            return $new_route;
            }   
  • Make it a class-level variable? Return the value from the function and use the returned value? It's not clear what you *should* be doing without seeing where the value needs to be used. But there are a few different ways to pass values around. – David Aug 15 '17 at 16:07

1 Answers1

0

If you want to execute the function you will have to either use another file e.g. run.php or you can run the function at the bottom of your class so for example:

class your_class {
public function getRoute() {
                 $find_route = $_SERVER['QUERY_STRING'];
             $get_route = explode('&',$find_route);
            $set_route = explode('/',$get_route[0]);
             $new_route = $set_route[1];    
                if ($new_route == 'memberships'){
                    $new_route = "MEMBER";  
                }elseif ($new_route == 'donations'){
                    $new_route = "DONATE";
                }elseif ($new_route == 'tickets'){
                    $new_route = "TICKET";
                }elseif ($new_route == 'items'){
                    $new_route = "ITEM";
                }elseif ($new_route == 'peer'){
                    $new_route = "PEER";
                }elseif ($new_route == 'recurring'){
                    $new_route = "RECURRING";
                }
            return $new_route;
      }   
}
$this->getRoute();

Then the output should be the correct output :)

However I would advise you use another file which has the class required and used e.g.

<?php
   require_once('class.php');
   $class = new class();
   $getRoute = $class->getRoute();
   print_r($getRoute); // For testing the code
?>
Tom
  • 389
  • 2
  • 15