Namespaces FAQ

We now have an implementation of namespaces in PHP 6 HEAD, so here’s a short FAQ about how they work for those that are too laz^H^H^Hbusy to read the whole README.namespaces.

Q. Why PHP needs namespaces?
A. Because long names like PEAR_Form_Loader_Validate_Table_Element_Validator_Exception are really tiresome.

Q. What is the main goal of the namespace implementation?
A. To solve the problem above.

Q. What “namespace X::Y::Z” means?
A: 1. All class/function/method names are prefixed with X::Y::Z.
2. All class/function/method names are resolved first against X::Y::Z.

Q. What “import X::Y::Z as Foo” means?
A. Every time there’s Foo as a class/function name or prefix to the name, it really means X::Y::Z

Q. What “import X::Y::Z” means?
A. “import X::Y::Z as Z”, then see above.

Q. What “import Foo” means?
A. Nothing.

Q. What is the scope of namespace and import?
A. Current file.

Q. Can same namespace be used in multiple files?
A. Yes.

Q. Is there any relation between namespaces X::Y::Z and X::Y?
A. Only in programmer’s mind.

Q. How do I import all classes from namespace X::Y::Z into global space?
A. You don’t, since it brings back the global space pollution problem.
Instead, you import X::Y::Z and then prefix your classes with Z::.

Q. But doesn’t it mean I will still have long names?
A. Not longer then three elements: Namespace::Class::Element.

Q. Why it is not implemented like in <insert your favorite language here>?
A. Because PHP is not <insert your favorite language here> ;)

Also we are considering to add one more feature to namespaces – ability to declare a namespaced constant – i.e. constant named Name::Space::NAME – with same resolution rules like classes – with const operator. Consequently it may be also possible to have const NAME = ‘value’ in global context, meaning the same as define(‘NAME’, ‘value’).

Also note namespaces are still work in progress, so it may happen it would be changed a lot when it’s released.

About these ads

12 thoughts on “Namespaces FAQ

  1. Pingback: What is a namespace and how is it implemented in PHP? - PHP Solutions - Developers Q & A

  2. Pingback: What is a namespace and how is it implemented in PHP? - PHP Solutions - Developers Q & A

  3. Namespaces are a band-aid for poor programming. Really all that needs to be done is to include versioning in your class names. Besides, after you require and declare the instance of the class you can call it anything you like.. ie.

    // declaration
    require_once PEAR_Form_Loader_Validate_Table_Element_Validator_Exception.php;
    $O = new PEAR_Form_Loader_Validate_Table_Element_Validator_Exception();
    // now use it

    • I’m afraid I disagree. Namespaces can be used wrongly, but in general they mean exactly to avoid names like PEAR_Form_Loader_Validate_Table_Element_Validator_Exception as much as possible.

  4. Pingback: 網站製作學習誌 » [PHP] 幾篇不錯的文章

  5. Pingback: PHP 5.3.0 Released! «

  6. Question (and please merge with comment posted earlier) – now that namespaces are implemented, would it be possible to move the HUGE number of inbuilt functions of PHP into namespaces and only import what the script will need at start? Now in order to keep from breaking older applications a legacy mode would be needed, but could we go to legacy mode on the fly??

    Something like this. Say I call to function mysql_connect. Formally, this library is in the mysql namespace, but it is also in the legacy namespace. Whenever a function is called for that isn’t in the currently loaded namespace, before failing the Zend parser would check the legacy namespace containing all functions from PHP 5 or prior that didn’t exist. This check may cost a few clock cycles but the speed on the applications written purely for PHP 6 would make it worth it since PHP 6 applications would only need to require the namespace libraries of PHP they intended to use. Following in this example, I really don’t need the memory overhead of the postOgre SQL libraries if I’m using mySQL (and the reverse is also true for those postOgre fans out there).

  7. Finally – a truly useful feature to drive migration to PHP 6 that more than offsets (albeit needed for security reasons) functionality loss of register_globals, magic_quotes etc.

  8. Pingback: Namespaces no PHP 6

  9. Pingback: » PHP 10.0 Blog: Namespaces FAQ

  10. Pingback:

  11. Pingback: RonnieWeb | Weblog » Jmenné prostory v PHP 6

Comments are closed.