I'm facing a strange way to call a method on a object.
$controller->{ $action }();
But if i remove the curly braces, the call will work anyway. Someone knows what those curly braces mean?
The current context
<?php
function call($controller, $action) {
// require the file that matches the controller name
require_once('controllers/' . $controller . '_controller.php');
// create a new instance of the needed controller
switch($controller) {
case 'pages':
$controller = new PagesController();
break;
}
// call the action
$controller->{ $action }();
}
// just a list of the controllers we have and their actions
// we consider those "allowed" values
$controllers = array('pages' => ['home', 'error']);
// check that the requested controller and action are both allowed
// if someone tries to access something else he will be redirected to the error action of the pages controller
if (array_key_exists($controller, $controllers)) {
if (in_array($action, $controllers[$controller])) {
call($controller, $action);
} else {
call('pages', 'error');
}
} else {
call('pages', 'error');
}
?>
UPDATE
$controller and $action are variables inherited from a index.php file which requires this one. So as inherited variables they are fully accessible.
Here's index.php
// set default controller and action
$controller = 'login';
$action = 'index';
// check if $_GET variables are set
if(isset($_GET['controller']) && $_GET['action'])
{
// if we have something set in here we override the default value
$controller = $_GET['controller'];
$controller = $_GET['action'];
}
// now we require the router file who will read the $controller and $action vars.
require_once '../app/core/Router.php';