第22行/home1/gcc/public_html/university/core/init.php中的解析错误:语法错误,意外T_FUNCTION,期待')'[重复]

问题描述:

Run the code in local host wamp, but when i uploaded to the server i got this error. Below is my code

<?php
session_start();

$GLOBALS['config'] = array(
    'mysql' => array(
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'db'=> 'database name'
    ),
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expiry' => 604800
    ),
    'session' => array(
        'session_name' => 'user',
        'token_name' => 'token',
        'secure' => rand(1000, 9999)
    )
);

spl_autoload_register(function($class) {            // error here
    require_once 'classes/' .$class. '.php';
});
</div>

It seems because you are running on an old PHP version (older than PHP 5.3), anonymous function are seen as syntax errors. PHP implements callbacks as string. You can define the function normally, and pass a name of the function instead.

function custom_autoloader($class) {
    // your code..
}

spl_autoload_register('custom_autoloader');

Your PHP version is not supporting anonymous function. You need at least 5.3.0

You can check your current version eg. using phpinfo().