1

I am trying to use a pre-written PHP autoloader and dreamweaver is stating that there is a syntax error. Can someone please explain what is causing the problem.

The lines which show as having syntax errors are $autoloader = new static($dir, $ext); spl_autoload_register([$autoloader, 'load']);

<?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 '';
    }
}
2
  • Please cut & paste the exact error messages you get, not just a paraphrase. Commented Apr 4, 2014 at 15:34
  • There is a syntax error on line 29. code hinting my not work until you correct this error Commented Apr 4, 2014 at 16:19

1 Answer 1

4
spl_autoload_register([$autoloader, 'load']);

This array syntax is only valid as of PHP 5.4. If DreamWeaver is using an older version of PHP syntax rules, it will complain.

Sign up to request clarification or add additional context in comments.

1 Comment

and it looks like static:: -> new static($dir, $ext) is valid as of PHP 5.3 (php.net/manual/en/language.oop5.late-static-bindings.php), so Dreamweaver might be < 5.3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.