0

I have a PHP script which is producing the following error

Parse error: syntax error, unexpected '[', expecting ')' in /home/masked/public_html/masked/AutoLoader.php on line 20

(note that I masked some parts of the directory given as they correspond with personal info.)

(line 20 is spl_autoload_register([$autoloader, 'load']); found in the register function.

I am running PHP 5.5 I have also tried it with PHP 5.3.27 and get the exact same result.

code bellow

run.php

require_once 'AutoLoader.php';
AutoLoader::register('src');

AutoLoader.php

/**
 * A basic PSR style autoloader
 */
class AutoLoader
{
    protected $dir;
    protected $ext;

    public function __construct($dir, $ext = '.php')
    {
        $this->dir = rtrim($dir, DIRECTORY_SEPARATOR);
        $this->ext = $ext;
    }

    public static function register($dir, $ext = '.php')
    {
        $autoloader = new static($dir, $ext);
        spl_autoload_register([$autoloader, 'load']);

        return $autoloader;
    }

    public function load($class)
    {
        $dir = $this->dir;

        if ($ns = $this->get_namespace($class)) {
            $dir .= DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $ns);
        }

        $inc_file = $dir.DIRECTORY_SEPARATOR.$this->get_class($class).$this->ext;

        if (file_exists($inc_file)) {
            require_once $inc_file;
        }
    }

    // Borrowed from github.com/borisguery/Inflexible
    protected static function get_class($value)
    {
        $className = trim($value, '\\');

        if ($lastSeparator = strrpos($className, '\\')) {
            $className = substr($className, 1 + $lastSeparator);
        }

        return $className;
    }

    // Borrowed from github.com/borisguery/Inflexible
    public static function get_namespace($fqcn)
    {
        if ($lastSeparator = strrpos($fqcn, '\\')) {
            return trim(substr($fqcn, 0, $lastSeparator + 1), '\\');
        }

        return '';
    }

}
megaman
  • 1,035
  • 1
  • 14
  • 21
  • 2
    What version of PHP are you running? Please double check. Defining arrays using `[]` syntax (as per `spl_autoload_register([$autoloader, 'load']);`) requires PHP >= 5.4 – Mark Baker Apr 04 '14 at 22:14
  • Works fine [here](http://codepad.viper-7.com/j9Cw1l), though it's obviously a bit different from using two files. Still, are you sure there's nothing more in this code? – raina77ow Apr 04 '14 at 22:15
  • @MarkBaker 'I am running PHP 5.5'. But yes, the more I think about the more I'm sure that could be the only sensible reason. ) – raina77ow Apr 04 '14 at 22:17
  • 1
    @SamCoppock It may sound weird, but - could you add `echo phpversion()` to the very beginning of `run.php` file to check that's indeed PHP 5.5? – raina77ow Apr 04 '14 at 22:18
  • Check with `php -i | grep "PHP Version"` from cli or with phpinfo() from web – Mark Baker Apr 04 '14 at 22:19
  • 1
    Going from the other side: what happens when you replace `[$autoloader, 'load']` with `array($autoloader, 'load')`? – raina77ow Apr 04 '14 at 22:21
  • It *could* be a PHP bug, but it is more likely that you are mistaken about the PHP version. Open a terminal and check the version using `php -v` (or `echo phpversion();` in a PHP file). – Sverri M. Olsen Apr 04 '14 at 22:33
  • ah - my server was running 5.3.27 i must have pressed the wrong button in cpannel when i tried to change it – megaman Apr 04 '14 at 22:36
  • This brings me to a new question When you said "Defining arrays using [] syntax (as per spl_autoload_register([$autoloader, 'load']);" how else can you pass the same data into the function would you have to write an array with a handle and input that ie $array =['a'->$autoloader,'b'->'load']; spl_autoload_regester($array); – megaman Apr 04 '14 at 22:37
  • @SamCoppock: No, just like it was commented: http://stackoverflow.com/questions/22873856/a-php-runtime-error#comment34901383_22873856 - http://php.net/array – hakre Apr 05 '14 at 00:09
  • 1
    Additionally, this is a common error covered in the *PHP Error Reference* we keep here on Stackoverflow: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/22316776#22316776 – hakre Apr 05 '14 at 00:10

1 Answers1

0

On run.php try:

require_once('Autoloader.php');
Herland Cid
  • 574
  • 1
  • 6
  • 23