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

hakre
  • 193,403
  • 52
  • 435
  • 836
Dzung Nguyen
  • 9,152
  • 14
  • 65
  • 104
  • 2
    possible duplicate of [Calling a static method from a class in another namespace in PHP](http://stackoverflow.com/questions/6579040/calling-a-static-method-from-a-class-in-another-namespace-in-php) – Gordon Jul 06 '11 at 08:47
  • @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. – Gordon Jul 06 '11 at 09:02
  • @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! – Lightness Races in Orbit Jul 06 '11 at 09:04
  • @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. – Gordon Jul 06 '11 at 09:26
  • @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". – Lightness Races in Orbit Jul 06 '11 at 10:19
  • @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: http://blog.stackoverflow.com/2011/02/are-some-questions-too-simple/ - see the flow chart at the end. – Gordon Jul 06 '11 at 10:31
  • @Gordon: "General reference" is not the same as "Not a Real Question"! Whatever happened to "general reference", anyway? – Lightness Races in Orbit Jul 06 '11 at 10:58
  • @Tomalak [the corresponding answer on meta says "this close reason was implemented for testing on http://scifi.stackexchange.com and http://english.stackexchange.com"](http://meta.stackexchange.com/questions/86043/introduce-a-general-reference-close-reason), so it's not on the other Stack sites yet. However, I've seen mods close obvious questions as NARQ in the meantime. – Gordon Jul 06 '11 at 11:25
  • @Gordon: I still disapprove of the practice. :) – Lightness Races in Orbit Jul 06 '11 at 11:44
  • @Tomalak Fair enough. But it's also still a duplicate ;) **[Recursion]** – Gordon Jul 06 '11 at 11:47
  • @Gordon: Warning: "Going in circles" detected! :P – Lightness Races in Orbit Jul 06 '11 at 11:47

5 Answers5

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!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks man, I didn't know what is the key for search about this problem on manual :) – Dzung Nguyen Jul 06 '11 at 08:43
  • 5
    @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. – Lightness Races in Orbit Jul 06 '11 at 08:44
  • 2
    Thanks Tomalak, I feel misery at very first time I make this question. But maybe a whole day working with code makes me blinded :P – Dzung Nguyen Jul 06 '11 at 08:54
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.

hakre
  • 193,403
  • 52
  • 435
  • 836
3

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

namespace AbcException;
use Exception;

class AbcException extends Exception {
    // Your Code
}
Suf_Malek
  • 636
  • 6
  • 10
2

It's simply a blackslash. Like \Exception.

sallaigy
  • 1,524
  • 1
  • 13
  • 12
-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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
cybertig
  • 19
  • 5