WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform
Namespace definition

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.