Question

I just wrote this and was thinking there must be an easier/more clean way to do this as it looks terrible doing it this way:

if(isset($_GET['m']) && isset($_GET['v'])) {
    Router::Get($_GET['m'], $_GET['v']);
} elseif(isset($_GET['m'])) {
    Router::Get($_GET['m'], "");
} else {
    Router::Get("", "");
}

I'm looking for a cleaner way, like:

Router::Get(is('m'), is('v'));

Any suggestions to shorten/clean this kind of if-statements?

Was it helpful?

Solution 2

You can encapsulate the logic:

Router::$query = $_GET;
Router::Get('m', Router::Get('v'))

class Router {
    static public $query;
    static function Get($name, $default = '') {
        return isset(self::$query[$name]) ? self::$query[$name] : $default;
    }
}

OTHER TIPS

Can do,

$m = isset($_GET['m']) ? $_GET['m'] : "";
$v = isset($_GET['v']) ? $_GET['v'] : "";
Router::Get($m, $v);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top