0

I've got the following code snippet from http://requiremind.com/a-most-simple-php-mvc-beginners-tutorial/, however it throws an error:

Parse error: syntax error, unexpected '[' in /usr/home/domains/xyz.pl/public_html/my-soft/routes.php on line 20 on my hosting which has 5.2.17 PHP version.

I searched stackoverflow for a corresponding topic about 5.2 vs 5.4 array differences, but i coudldn't find anything meaningful so I can solve this particulat error (both tables are named), and final resolution should comply with this fact -> table names ('pages', 'posts') are used in conditional statement below the table intitialization.

Could you please advise me on how to solve this without changing the PHP version?

 <?php
  function call($controller, $action) {
    require_once('controllers/' . $controller . '_controller.php');

    switch($controller) {
      case 'pages':
        $controller = new PagesController();
      break;
      case 'posts':
        // we need the model to query the database later in the controller
        require_once('models/post.php');
        $controller = new PostsController();
      break;
    }

    $controller->{ $action }();
  }

  // we're adding an entry for the new controller and its actions
  $controllers = array('pages' => ['home', 'error'],
                       'posts' => ['index', 'show']);

  if (array_key_exists($controller, $controllers)) {
    if (in_array($action, $controllers[$controller])) {
      call($controller, $action);
    } else {
      call('pages', 'error');
    }
  } else {
    call('pages', 'error');
  }
?>
  • change `['home', 'error']` to `array('home','error')` and `['index', 'show']` to `array('index', 'show')` – coderodour Nov 15 '17 at 17:11
  • From **PHP5.4** it is possible to declare an array as `$array=[];`. Before that version, it has to be `$array=array();` – Michel Nov 15 '17 at 17:13
  • Specifically, https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them/29505827#29505827. – chris85 Nov 15 '17 at 17:23
  • Unfortunately there's nothing about subarrays naming, which is used after a few lines, so coderodur anser is not correct, while Gogeta70 answer gives a correct result. – user1978289 Nov 15 '17 at 17:46

1 Answers1

1

It looks like they're trying to make a multi-dimensional array.

Try changing the code to this:

$controllers = array('pages' => array('home', 'error'),
                   'posts' => array('index', 'show'));
Gogeta70
  • 881
  • 1
  • 9
  • 23
  • 1
    It has nothing to do with multidimensional. Simply OP's PHP version can't initialize an array with only brackets. – Michel Nov 15 '17 at 17:17
  • 1
    You misunderstood what I meant. I simply mean that they were inconsistent with their method of creating an array, and they **are** making a multi-dimensional array, which the OP didn't appear to understand. I was clarifying what the code does. – Gogeta70 Nov 15 '17 at 17:24
  • Works perfectly, thank you! – user1978289 Nov 15 '17 at 17:47