PHP get namespaces

July 14th, 2007 by Sadri Sahraoui | Print PHP get namespaces

The long awaited feature has been added to the PHP repository yesterday using the Dimitry Stogov’s implementation.
Define a namespace :

namespace Zend::DB;
class Connection
{
    //code
}
function connect()
{
    //code
}

You can import namespaces or class name like this:

require 'Zend/Db/Connection.php';
import Zend::DB;
import Zend::DB::Connection as DbConnection;
$x = new Zend::DB::Connection();
$y = new DB::connection();
$z = new DbConnection();
DB::connect();

 

2 Comments

  1. kow

    Hi,

    Why namespaces are better than just icluding a database class? It is just fog grouping, or what?

    How can I get advantage of namespaces?

    Thanks,
    A php4 programmer

  2. okworld

    The main advantage of using namespaces is to avoid naming collision. For example suppose that you work with the class DB from PEAR and for whatever reason you want to use the class DB written by yourself you’ll get in trouble quickly when you need to include the 2 classes, here come the role of namespaces . This is especially useful when you work with frameworks and components or SPL and they help to organize the code.

Trackbacks

Leave a Reply