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

search for in the

Global space> <Namespace definition
Last updated: Fri, 14 Nov 2008

view this page in

Using namespaces

Every class and function in a namespace can be referred to by the full name - e.g. MyProject::DB::Connection or MyProject::DB::connect - at any time.

Example #1 Using namespaced name

<?php
    
require 'MyProject/Db/Connection.php';
    
$x = new MyProject::DB::Connection;
    
MyProject::DB::connect();
?>

Namespaces can be imported into current context (global or namespace) using the use operator. The syntax for the operator is:

<?php
/* ... */
use Some::Name as Othername;

// The simplified form of use:
use Foo::Bar;
// which is the same as :
use Foo::Bar as Bar;
?>
The imported name works as follows: every time that the compiler encounters the local name Othername (as stand-alone name or as prefix to the longer name separated by ::) the imported name Some::Name is substituted instead.

use can be used only in global scope, not inside function or class. Imported names have effect from the point of import to the end of the current file. It is recommended to put imports at the beginning of the file to avoid confusion.

Example #2 Importing and accessing namespace

<?php
    
require 'MyProject/Db/Connection.php';
    use 
MyProject::DB;
    use 
MyProject::DB::Connection as DbConnection;
    
    
$x = new MyProject::DB::Connection();
    
$y = new DB::connection();
    
$z = new DbConnection();
    
DB::connect();
?>

Note: The import operation is compile-time only, all local names are converted to their full equivalents by the compiler. Note that it won't translate names in strings, so callbacks can't rely on import rules.



add a note add a note User Contributed Notes
Using namespaces
kukoman at pobox dot sk
17-Oct-2008 03:20
PHP 5.3.0alpha2 (cli)
<?php
//  namespace MyProject::DB;
require 'db.php';

use
MyProject::DB; // fine; same as DB::
use MyProject::DB::Connection as DBC; // fine
use MyProject::DB as HM; // fine
use HM::Connection as DBC2; // class call ends with FATAL!!!

$x = new DBC(); // fine
$y = new HM::Connection(); // fine
$z = new DBC2(); // Fatal error: Class 'HM::Connection' not found
?>
richard at richard-sumilang dot com
27-Mar-2008 06:36
Syntax for extending classes in namespaces is still the same.

Lets call this Object.php:

<?php

namespace com
::rsumilang::common;

class
Object{
  
// ... code ...
}

?>

And now lets create a class called String that extends object in String.php:

<?php

class String extends com::rsumilang::common::Object{
  
// ... code ...
}

?>

Now if you class String was defined in the same namespace as Object then you don't have to specify a full namespace path:

<?php

namespace com
::rsumilang::common;

class
String extends Object
{
  
// ... code ...
}

?>

Lastly, you can also alias a namespace name to use a shorter name for the class you are extending incase your class is in seperate namespace:

<?php

namespace com
::rsumilang::util;
use
com::rsumlang::common as Common;

class
String extends Common::Object
{
  
// ... code ...
}

?>

- Richard Sumilang

Global space> <Namespace definition
Last updated: Fri, 14 Nov 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites