downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  

<NamespacesUsing namespaces>
Last updated: Thu, 26 Jun 2008

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.



add a noteadd a note User Contributed Notes
Namespace definition
There are no user contributed notes for this page.




<NamespacesUsing namespaces>
Last updated: Thu, 26 Jun 2008
show source | credits | sitemap | contact | advertising | mirror sites
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://phpbuilder.com/
Last updated: Tue Nov 1 20:20:59 2005 EST
Columns / Articles | Tips / Quickies | News | News Linking and RSS Feeds | Shared Code Library
Mail Archives | Support / Discussion Forums | Get Started! Links | Contribute! | Docs