PHP 5.2 - 如何解决在使用 MVC 初学者教程示例时抛出的解析语法错误?
PHP 5.2 - How can I solve a parse-syntax error thrown when using MVC beginner tutorial example?
我从 http://requiremind.com/a-most-simple-php-mvc-beginners-tutorial/ 中获得了以下代码片段,但它会引发错误:
Parse error: syntax error, unexpected '[' in /usr/home/domains/xyz.pl/public_html/my-soft/routes.php on line 20
在我的主机上有 5.2.17 PHP 版本。
我在 Whosebug 上搜索了一个关于 5.2 和 5.4 数组差异的相应主题,但是我找不到任何有意义的东西所以我可以解决这个特殊错误(两个 table 都被命名),最终解决方案应该符合这一事实 -> table 名称('pages'、'posts')用于 table 初始化下方的条件语句。
你能告诉我如何在不更改 PHP 版本的情况下解决这个问题吗?
<?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');
}
?>
看起来他们正在尝试制作一个多维数组。
尝试将代码更改为:
$controllers = array('pages' => array('home', 'error'),
'posts' => array('index', 'show'));
我从 http://requiremind.com/a-most-simple-php-mvc-beginners-tutorial/ 中获得了以下代码片段,但它会引发错误:
Parse error: syntax error, unexpected '[' in /usr/home/domains/xyz.pl/public_html/my-soft/routes.php on line 20
在我的主机上有 5.2.17 PHP 版本。
我在 Whosebug 上搜索了一个关于 5.2 和 5.4 数组差异的相应主题,但是我找不到任何有意义的东西所以我可以解决这个特殊错误(两个 table 都被命名),最终解决方案应该符合这一事实 -> table 名称('pages'、'posts')用于 table 初始化下方的条件语句。
你能告诉我如何在不更改 PHP 版本的情况下解决这个问题吗?
<?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');
}
?>
看起来他们正在尝试制作一个多维数组。
尝试将代码更改为:
$controllers = array('pages' => array('home', 'error'),
'posts' => array('index', 'show'));