PHP 10.0 Blog

What if…

Namespaces FAQ

Posted by Stas on August 17, 2007

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 Responses to “Namespaces FAQ”

  1. [...] – Namespace PHP 6 FAQ – PHP Namespaces (Part 1: Basic usege & gotchas) – Namespaces [...]

  2. PHP 10.0 Blog: Namespaces FAQ

  3. [...] PHP 10.0 Blog has the quick and dirty version posted today of the full namespaces readme that’s been posted about the upcoming [...]

  4. [...] – e procurando coisas novas sobre PHP – achei um post interessante no blog PHP 10.0. É um FAQ sobre como vai funcionar namespaces no PHP 6. Tudo bem, [...]

  5. Michael said

    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.

  6. Michael said

    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. [...] What’s in: A bunch of minor fixes aaand: Namespaces – so we can section off bits of code properly. Woohoo! more info here. [...]

  8. [...] Namespaces FAQ [...]

  9. Dave said

    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

    • Stas said

      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.

  10. […] example given here is of the exception […]

  11. […] example given here is of the exception […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: