41

I get a parse error when trying to use a name space inside my own function

require('/var/load.php');

function go(){

  use test\Class;

    $go = 'ok';
    return $go;
}

    echo go();
3
  • 1
    PHP Parse error: syntax error, unexpected T_USE in /var/www/test.php on line 8 Commented Aug 14, 2013 at 9:07
  • 2
    Why do you want to do this to begin with? use just establishes a file-wide alias name, nothing more, nothing less. What's the point of doing this in a function? Commented Aug 14, 2013 at 9:18
  • This was my concern: "In PHP, using a namespace declaration does not consume memory in the way that declaring a variable or creating an object would. Rather, it helps organize code by grouping related classes, functions, and constants together under a specific name, avoiding name conflicts in large applications." – So you can declare namespaces without memory issues. Commented May 13 at 5:32

3 Answers 3

63

From Scoping rules for importing

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

So you should put like this, use should specified at the global level

require('/var/load.php');
use test\Class;

function go(){
    $go = 'ok';
    return $go;
}
echo go();

Check the example 5 in the below manual Please refer to its manual at http://php.net/manual/en/language.namespaces.importing.php

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

5 Comments

Namespaces have nothing to do with scope, so I'd avoid the ambiguous use of that term here...
@deceze please specify some link for clear understanding of namespaces and scope in php
...? You've linked to one: the manual. Variable scope and namespaces are simply two entirely different concepts.
@deceze as per my understanding, namespaces are same as 'alias' command in unix, but then why it has only global scope? why cant I define it in a function, with scope limited to that function only? Please help in understanding this
I suppose because the use of that would be very limited and it would introduce confusion. In one function you use Foo\Bar as MyClass yet in another function within the same file you use Foo\Baz as MyClass. That easily introduces very confusing little mistakes when reading source code. Therefore use directives apply to a whole file/namespace at once. I have yet to encounter a case where this is not sufficient anyway.
7

From the manual:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations.

3 Comments

@westnblue It means that a use statement can only occur in the part of the code that is run as soon as the file is opened. If you put it inside a class or a function, it would execute when the class or function is invoked. This is incompatible with the PHP system for namespaces and parsing your code, so you have to put the use statement in the outermost scope of the file.
save me the headache of figuring out the problem with my namespace that I put "use" in a function. thanks
But what if you have complex code where you cannot "touch" the global scope but can only provide a function for the project you are working on?
1

From what I gather a lot of people are getting this when including a separate function file and trying to use the static method inside that function.. For example in index.php

namespace foo/bar
require('func.php')
f();

and in func.php

function f() {
    StaticClass::static_method();
}

you simply need to declare namespace foo/bar in func.php (same like how you declared it in index.php) so instead of the above it should look like:

namespace foo\bar
function f() {
    StaticClass::static_method();
}

to avoid errors like:

Fatal error: Uncaught Error: Class 'StaticClass' not found in func.php

It's obvious now but I was confused why func.php does not carry over the namespace declaration inside the file that 'requires' func.php

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.