3

I'm trying to make the following work:

function flosoftdedicated_api_init() {
require_once 'resources/vendor/autoload.php';
use \Ovh\Common\Ovh;

....

$ovh = new Ovh($config);
return $ovh;
}

function flosoftdedicated_ClientArea($params) {
global $ovh;

$ovh = flosoftdedicated_api_init();
....
}

but I get the error:

Parse error: syntax error, unexpected T_USE in ..../flosoftdedicated.php on line 35 Line 35 being the use statement.

Is it not possible to use a namespace in a function? Do I need to assign the same namespace?

1

2 Answers 2

12

It's because you cannot declare it from within a function. From PHP: Using Namespaces:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

You'll have to move it outside of any function or class.

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

Comments

0

The "use" keyword can not be declared inside the function or method.

from php.net http://www.php.net/manual/en/language.namespaces.importing.php#98908

Comments

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.