rfc:namespaceresolution

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
rfc:namespaceresolution [2008/11/04 19:18]
lsmith
rfc:namespaceresolution [2011/04/06 10:59]
127.0.0.1 external edit
Line 3: Line 3:
   * Date: 2008-10-30   * Date: 2008-10-30
   * Author: Lukas Smith <smith@pooteeweet.org>   * Author: Lukas Smith <smith@pooteeweet.org>
-  * Status: Under Discussion+  * Status: Accepted
   * First Published at: http://wiki.php.net/rfc/namespaceresolution   * First Published at: http://wiki.php.net/rfc/namespaceresolution
  
 This RFC discusses the way identifiers inside a namespace are to be resolved that are not fully qualified. This RFC discusses the way identifiers inside a namespace are to be resolved that are not fully qualified.
 +
 ===== Introduction ===== ===== Introduction =====
  
Line 37: Line 38:
  
 ===== Possible approaches ===== ===== Possible approaches =====
- 
 ==== Fallback to the global namespace ==== ==== Fallback to the global namespace ====
  
 In this scenario when an identifier does not resolve inside the current namespace and attempt would be made to find the identifier in the global namespace. In this scenario when an identifier does not resolve inside the current namespace and attempt would be made to find the identifier in the global namespace.
  
-When referencing global identifiers in namespaces, it is probably a reasonable assumption that the bulk of that will be function calls. This is because currently most functionality in PHP is provided via functions. Also once an instance of a class has been made, one does not have to reference the identifier again in common usage (obviously there will still be cases, like with instanceof/type hint checks or static calls).+When referencing global identifiers in namespaces, it is probably a reasonable assumption that the bulk of that will be function calls. This is because currently most functionality in PHP is provided via functions. Also once an instance of a class has been made, one does not have to reference the identifier again in common usage (there will still be cases, like with instanceof/type hint checks or static calls).
  
 In the past people often created classes for the sole reason of being able to sort of "namespace" their functions. Given that we now have real namespaces, class usage as a namespace replacement is no longer necessary. Still another possible assumption, which is considerably more dangerous to make, is that most code that uses namespaces will mostly use classes for its implementation and considerably less depend on namespaced functions. In the past people often created classes for the sole reason of being able to sort of "namespace" their functions. Given that we now have real namespaces, class usage as a namespace replacement is no longer necessary. Still another possible assumption, which is considerably more dangerous to make, is that most code that uses namespaces will mostly use classes for its implementation and considerably less depend on namespaced functions.
Line 48: Line 48:
 One noteworthy aspect here is that for classes we have autoload. If non fully qualified identifiers can be used to reference global identifiers, "lazy" programmers can skip fully qualifying identifiers even if they have the full intention of referencing a global identifier. With autoload this could trigger expensive operations, which are essentially useless. One noteworthy aspect here is that for classes we have autoload. If non fully qualified identifiers can be used to reference global identifiers, "lazy" programmers can skip fully qualifying identifiers even if they have the full intention of referencing a global identifier. With autoload this could trigger expensive operations, which are essentially useless.
  
-For functions however we do not have autoload capabilities. This brings the advantage that falling back to the global namespace does not run the performance risk of autoload. So a fallback would be much less expensive, but obviously there would still be overhead for not making intentional references to the global namespace fully qualified.+For functions however we do not have autoload capabilities. This brings the advantage that falling back to the global namespace does not run the performance risk of autoload. So a fallback would be much less expensive, but there would still be overhead for not making intentional references to the global namespace fully qualified.
  
 At the same time the ability to automatically fallback into the global namespace gives the ability to overload global identifiers inside a namespace without having to modify existing code inside that namespace. This however can also be considered dangerously similar to the ambiguity issues we solved by changing the namespace separator (for example static code analysis becomes more difficult). At the same time the ability to automatically fallback into the global namespace gives the ability to overload global identifiers inside a namespace without having to modify existing code inside that namespace. This however can also be considered dangerously similar to the ambiguity issues we solved by changing the namespace separator (for example static code analysis becomes more difficult).
Line 72: Line 72:
 </code> </code>
  
-One approach to make it at least noticeable when a fallback into the global namespace occurs would be to throw an E_NOTICE in this case. This would obviously discourage users from using the fallback for overloading, but it would ensure that people migrating legacy code or people who have not yet fully understood namespaces, would be able to find out about where they are loosing performance.+One approach to make it at least noticeable when a fallback into the global namespace occurs would be to throw an E_NOTICE in this case. This would discourage users from using the fallback for overloading, but it would ensure that people migrating legacy code or people who have not yet fully understood namespaces, would be able to find out about where they are loosing performance.
  
 Another approach to reduce (though it does not remove the issues entirely) some of the issues is by simply removing functions (and constants) from namespaces. Another approach to reduce (though it does not remove the issues entirely) some of the issues is by simply removing functions (and constants) from namespaces.
rfc/namespaceresolution.txt · Last modified: 2017/09/22 13:28 (external edit)