Home > database >  PHP Anonymous Function doesn't want to execute
PHP Anonymous Function doesn't want to execute

Time:03-12

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.

CodePudding user response:

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');
});
  •  Tags:  
  • php
  • Related