Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "root" namespace of php?

Tags:

namespaces

php

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

like image 583
Dzung Nguyen Avatar asked Jul 06 '11 08:07

Dzung Nguyen


People also ask

How can I access namespace in PHP?

The first thing to do is to define a namespace with the namespace keyword at the top of the PHP file. All the code underneath the namespace keyword becomes namespaced code. It's important to note that this keyword must be placed at the top of the file, making sure that nothing precedes it.

What is the use of namespace in PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

Can I use two namespace in PHP?

Defining multiple namespaces in the same file ¶Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.

What is the difference between namespace and use in PHP?

A namespace is a way of grouping identifiers so that they don't clash. Using a class implies that you can create an instance of that class, not true with namespaces. 2. You can use using-declarations with namespaces, and that's not possible with classes unless you derive from them.


2 Answers

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!

like image 101
Lightness Races in Orbit Avatar answered Oct 05 '22 11:10

Lightness Races in Orbit


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.

like image 43
hakre Avatar answered Oct 05 '22 11:10

hakre