5

According to the PHP documentation

PHP namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. Note that importing a function or constant is not supported.

Does anyone know the historical or technical context of why importing a function or constant is unsupported?

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Those are basically the same restrictions that C# has. You can't "import" or "alias" a single function or constant in that language either. Why would you want to? – Robert Harvey Jul 01 '13 at 19:25
  • @RobertHarvey Writing code/systems in a style similar to python. – Alana Storm Jul 01 '13 at 19:27
  • 3
    @Robert Because it can be more convenient? `bar()` instead of `Foo\bar()`... – deceze Jul 01 '13 at 19:28
  • Just guessing (therefor not an answer): functions which are on there own aren' t very OOP-ish. Namespaces are ment to make PHP more OO. Same goes for constants. The whole namespace things is just for OOP and not for basic PHP things like functions. But still... just guessing. – DAG Jul 01 '13 at 19:28
  • 2
    @AlanStorm PHP is a curly-brace language with interfaces, is it not? Python is quite different in its approach to programming. C# makes this a bit easier with *extension methods.* – Robert Harvey Jul 01 '13 at 19:29
  • @RobertHarvey All valid points, and if you'd like to have a longer discussion I'm open to a chat (i.e. not here). However, I'm interested in the discussion around this that happened inside the PHP core team OR opinions from people with knowledge of PHP internals. Was it a deliberate decision to move PHP more towards Java/C# style coding patterns, was it a "hard to implement due to PHP's architecture" problem, etc. – Alana Storm Jul 01 '13 at 19:38
  • Perhaps you can make it clearer how this solves a practical programming problem you are facing, and isn't merely a question of curiosity. The only folks likely to have a genuine answer are the folks who designed PHP. Have you tried asking them? – Robert Harvey Jul 01 '13 at 19:39
  • I've pinged the folks most likely to know the answer in the [PHP chat room](http://chat.stackoverflow.com/rooms/11/php). – Robert Harvey Jul 01 '13 at 19:50
  • 3
    https://wiki.php.net/rfc/namespaces (9. Importing functions) – yannis Jul 01 '13 at 19:52

1 Answers1

1

I contacted Jochem Maas (the author of this five year old RFC), and while he was hesitant to pin point a single reason (understandably, as he isn't currently deeply involved with the namespace system), his three factors were

  1. Class name collisions were more of a real world problem than function name collisions

  2. PHP functions and classes live in different areas of the engine code, and there were technical hurdles to parsing out which was which during a use statement.

  3. There was some uncertainty/difference of opinion on how to handle the autoloader and the importing/aliasing of specific functions. (The autoloader, a separate system, works with classes only)

In the end, PHP's pragmatism won out, and that's why we have the namespace system we have today.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599