65963

Using “use” within a function?

Question:

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:

<blockquote>

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

</blockquote>

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

Answer1:

It's because you cannot declare it from within a function. From <a href="http://php.net/manual/en/language.namespaces.importing.php" rel="nofollow">PHP: Using Namespaces</a>:

<blockquote>

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.

</blockquote>

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

Answer2:

<blockquote>

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

</blockquote>

from php.net <a href="http://www.php.net/manual/en/language.namespaces.importing.php#98908" rel="nofollow">http://www.php.net/manual/en/language.namespaces.importing.php#98908</a>

Recommend

  • How to access COM objects from different apartment models?
  • Cannot run nunit tests with Nant
  • WPF and background worker and the calling thread must be STA
  • Docker container doesn't start, showing as 'Exited n seconds ago'
  • C# OpenFileDialog Thread start but dialog not shown
  • Single virtual attribute definition for multiple fields
  • Rails 3, Custom Actions, and HTML request methods
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • android Navigation Bar hiding and persantage of usable screen overlap
  • IE6 changes DOCTYPE to a bad one
  • ZipList with Scalaz
  • Connecting bluetooth device to windows phone 8 application
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How to access meteor package name inside package?
  • Updating Dojo provide
  • Defined variables not working in javascript files when I use getScript
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Unexpected token ILLEGAL while running node.js mocha test
  • Question about instantiating object
  • How can I display the parent menu item's description using Wordpress walkers?
  • Rails Find when some params will be blank
  • Email verification using google app script and google forms
  • Is there a javascript serializer for JSON.Net?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • Master page gives error
  • Azure Cloud Service Web Role web pages do not load
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Is there a mandatory requirement to switch app.yaml?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass