PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Using namespaces> <Namespaces
Last updated: Fri, 18 Jul 2008

view this page in

Namespace definition

The namespace is declared using namespace keyword, which should be at the very beginning of the file. Example:

Example #1 Defining namespace

<?php
    namespace MyProject
::DB;
    
    const 
CONNECT_OK 1;

    class 
Connection /* ... */ }
    
    function 
connect() { /* ... */  }
    
?>
Same namespace name can be used in multiple files.

Namespace can contain class, constant and function definitions, but no free code.

Namespace definition does the following:

  • Inside namespace, all class, function and constant names in definitions are automatically prefixed with namespace name. The class name is always the full name, i.e. in the example above the class is called MyProject::DB::Connection.
  • Constant definitions create constant which is composed of namespace name and constant name. Like class constants, namespace constant can only contains static values.
  • Unqualified class name (i.e., name not containing ::) is resolved at runtime following this procedure:

    1. Class is looked up inside the current namespace (i.e. prefixing the name with the current namespace name) without attempting to autoload.
    2. Class is looked up inside the global namespace without attempting to autoload.
    3. Autoloading for name in current namespace is attempted.
    4. If previous failed, lookup fails.

  • Unqualified function name (i.e., name not containing ::) is looked up at runtime first in the current namespace and then in the global space.

  • Unqualified constant names are looked up first at current namespace and then among globally defined constants.

See also the full name resolution rules.



Using namespaces> <Namespaces
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
Namespace definition
David Drakard
07-Sep-2008 03:56
I agree with SR, the new namespaces feature has solved a number of problems for me which would have required horrible coding to solve otherwise.

An example use:
Say you are making a small script, and write a class to connect to a database, calling it 'connection'. If you find your script useful and gradually expand it into a large application, you may want to rename the class. Without namespaces, you have to change the name and every reference to it (say in inheriting objects), possibly creating a load of bugs. With namespaces you can drop the related classes into a namespace with one line of code, and less chance of errors.

This is by no means one of the biggest problems namespaces solve; I would suggest reading about their advantages before citicising them. They provide an elegant solutions to several problems involved in creating complex systems.
Baptiste
14-May-2008 10:47
There is nothing wrong with PHP namespaces, except that those 2 instructions give a false impression of package management.
... while they just correspond to the "with()" instruction of Javascript.

By contrast, a package is a namespace for its members, but it offers more (like deployment facilities), and a compiler knows exactly what classes are in a package, and where to find them.
Anonymous
01-Apr-2008 10:11
@ RS: Also, you can specify how your __autoload() function looks for the files. That way another users namespace classes cannot overwrite yours unless they replace your file specifically.

Using namespaces> <Namespaces
Last updated: Fri, 18 Jul 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites