'PHP Anonymous Function doesn't want to execute [duplicate]

I'm recreating parts of Laravel in plain PHP for a school project and I've been trying to make the following code work. I've been trying to get an output, but I don't seem to get it right. What am I doing wrong?

class Route {
    public static function get($path) {
        return $path;
    }
}

function view($val)  {
    require_once $val . '.php';
}

Route::get('/', function () {
    return view('console');
});

The following code should include console.php, but it does nothing.

php


Solution 1:[1]

You need to update your get() parameters to accept a second parameter, and then call that function - I believe you can do this like $function(). See https://www.php.net/manual/en/functions.anonymous.php for more examples.

class Route {
    public static function get($path, $function) {
        $function();
        return $path;
    }
}

function view($val)  {
    require_once $val . '.php';
}

Route::get('/', function () {
    return view('console');
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WOUNDEDStevenJones