Unexpected 'use' (T_USE) error when using autoload

Asked

Viewed 6,287 times

4

I have the following folder and file structure:

-api
  -v1
  -libs
    -Slim
    -Facebook
      -autoload.php
  -index.php
  -login.php

Inside the "index.php" I do the include of "login.php":

require_once 'login.php';

Inside "login.php" I do the include of the file "libs/Facebook/autoload.php" and then try to use a class:

require_once 'libs/Facebook/autoload.php';
use Facebook\FacebookSession;

The following error is returned:

Parse error: syntax error, Unexpected 'use' (T_USE)

"autoload.php" contains the following code:

spl_autoload_register(function ($class){
   $prefix = 'Facebook\\';
   $base_dir = __DIR__;
   $len = strlen($prefix);
   if (strncmp($prefix, $class, $len) !== 0) {
      return;
   }
   $relative_class = substr($class, $len);
   $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
   if (file_exists($file)) {
      require $file;
   }
});

What I’m doing wrong so the autoload doesn’t work properly?

  • The use Facebook\FacebookSession; is being called within a method or function?

  • What version of php you are using?

1 answer

6


As this response from Soen:

This is because you probably set the namespace within a method or function, read on PHP: Using Namespaces:

The use must be declared in the outermost scope of a file (in global reach) or within declarations namespace. This is because the import is done at compile time and not run time, so it cannot be block scope.

To resolve you must move the use out of any function or class.

Supposing you’re like this:

function initiate () {
    require_once 'libs/Facebook/autoload.php';
    use Facebook\FacebookSession;
    ...
}

Do this:

require_once 'libs/Facebook/autoload.php';
use Facebook\FacebookSession;

function initiate () {
    ...
}
  • Yes, I was, I changed the require_once 'libs/Facebook/autoload.php'; and use Facebook Facebooksession; to the "index.php" out of function. Now the following error: Class 'Facebooksession' not found in...

  • Not solved, autoload still does not work, return the following error: Class 'Facebooksession' not found in C: wamp www iassistu api v1 login.php on line 22 tem Facebooksession::setDefaultApplication(FACEBOK_APP_ID,FACEBOOK_APP_SECRET);

  • @Filipemoraes A is the first problem solved, this is now a new problem, so I see autoload.php is not loading the class. Edit the question and post the whole content of the autoload.php and the content of login.php

  • Yes it is loading, I inserted the line Facebooksession::setDefaultApplication inside the index.php and it worked, inside the login.php.

  • 1

    Yeah, it’s settled. I inserted the "use" in index.php and login.php to use the class I have to put the "Facebook" before: Facebook Facebooksession::setDefaultApplication(FACEBOOK_APP_ID,FACEBOOK_APP_SECRET);

  • @Filipemoraes :) Thank you for contributing this information and for accepting my reply

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.