How to use "root" namespace of php?
Asked Answered
L

5

29

I have an Exception class:

namespace abc;

class AbcException extends Exception {
// blah blah
}

It produces this error:

Class 'abc\Exception' not found ...

Questions:

  1. What can I do to make this work ?

  2. Useful documents are appreciated.

Thanks for reading my question

Lees answered 6/7, 2011 at 8:37 Comment(11)
possible duplicate of Calling a static method from a class in another namespace in PHPFablan
@Tomalak dont judge a book by its cover. The OP in that question asks "How do I reference a class that belongs to no namespace, from inside one ?" which is exactly the same as this OP's question.Fablan
@Gordon: It's certainly highly related and on the same topic, but I think that the two look different enough for closing this one as a dup to be harmful. It's not as if this topic has been covered over and over on SO like some of the other topics that invoke knee-jerk close votes!Nonrecognition
@Tomalak It's not highly related. It's the same :) The answer is "prepend with a backslash". Yes, the scenario is different, but the problem is identical. It's just like all these questions asking how to parse all A elements from a webpage are the same as those asking how to parse all LI elements from a webpage. Besides, like you already pointed out this is easy to find by googling and thus the question qualifies for Not A Real Question anyway.Fablan
@Gordon: Just because the answer is the same does not mean the question is the same. And just because the answer is available on the internet doesn't mean that it's "not a real question".Nonrecognition
@Tomalak The problem is the same. Again: the scenario may be different (calling static method from global namespace versus extending class from global namespace). But the underlying problem for both scenarios is how to reference the global namespace. Anything else from these scenarios isnt important. It's not about static or inheritance. It's just about the namespace. And yes, it's not a real question because the official take is some questions are too simple: blog.stackoverflow.com/2011/02/are-some-questions-too-simple - see the flow chart at the end.Fablan
@Gordon: "General reference" is not the same as "Not a Real Question"! Whatever happened to "general reference", anyway?Nonrecognition
@Tomalak the corresponding answer on meta says "this close reason was implemented for testing on http://scifi.stackexchange.com and http://english.stackexchange.com", so it's not on the other Stack sites yet. However, I've seen mods close obvious questions as NARQ in the meantime.Fablan
@Gordon: I still disapprove of the practice. :)Nonrecognition
@Tomalak Fair enough. But it's also still a duplicate ;) [Recursion]Fablan
@Gordon: Warning: "Going in circles" detected! :PNonrecognition
N
29

What can I do to make this work ?

Use a leading backslash to indicate the global namespace:

namespace abc;

class AbcException extends \Exception {
// blah blah
}

Useful documents are appreciated.

There's an entire page devoted to this in the PHP manual!

Nonrecognition answered 6/7, 2011 at 8:40 Comment(3)
Thanks man, I didn't know what is the key for search about this problem on manual :)Lees
@nXqd: I googled for "namespace php", went to the first result, then clicked on "Using namespaces: fallback to global function/constant" which completely describes what you're trying to do! Really, you should have read the entire manual section on namespaces at some point, before starting to use this language feature.Nonrecognition
Thanks Tomalak, I feel misery at very first time I make this question. But maybe a whole day working with code makes me blinded :PLees
I
17

The Exception class is resolved to your scripts namespace (PHP Manual) as it starts with:

namespace abc;

You can specifically tell the script which exception to use then:

namespace abc;
use Exception;

class AbcException extends Exception {
// blah blah
}

With this variant you see on top of the file which classes you "import". Additionally you can later on more easily change/alias each Exception class in the file. See also Name resolution rules in the PHP Manual.

Alternatively you can specify the concrete namespace whenever you specify a classname. The root namespace is \, so the fully qualified classname for exception is \Exception:

namespace abc;

class AbcException extends \Exception {
// blah blah
}

This just works ever where, however, it makes your code more bound to concrete classnames which might not be wanted if the codebase grows and you start to refactor your code.

Inboard answered 6/7, 2011 at 8:41 Comment(0)
H
3

It's good to use "use" when including/extending other class OR libraries.

namespace AbcException;
use Exception;

class AbcException extends Exception {
    // Your Code
}
Hopi answered 1/2, 2016 at 7:56 Comment(0)
P
2

It's simply a blackslash. Like \Exception.

Placet answered 6/7, 2011 at 8:38 Comment(0)
H
-1

Under windows you don't need the leading \ to denote the root namespace. Once you deploy to a *nix environment you will have to explicitly add the \ in front of the class name whenever you use it or put the use statement for each root namespace class you are using before you use them, otherwise you'll get a class not found fatal error.

Hasten answered 9/2, 2021 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.