rfc:namespaceresolution
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:namespaceresolution [2008/10/30 10:51] – created lsmith | rfc:namespaceresolution [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | * Version: | + | * Version: |
* Date: 2008-10-30 | * Date: 2008-10-30 | ||
* Author: Lukas Smith < | * Author: Lukas Smith < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
- | This RFC discusses the way identifiers inside a namespace are resolved. | + | This RFC discusses the way identifiers inside a namespace are to be resolved |
===== Introduction ===== | ===== Introduction ===== | ||
- | Generally in namespaces we support fully qualified names. However what happens if a non fully qualified name is used that is not defined inside the namespace? Should this cause a fatal error or should an attempt be made to resolve this call to the global namespace? | + | Generally in namespaces we support fully qualified names as well as importing namespaces via " |
- | ==== Why do we need RFCs? ==== | + | <code php> |
+ | <?php | ||
+ | namespace foo; | ||
- | Obviously its important that we make a conscious decision for these questions. Depending on how we approach this, users might unintentionally trigger __autoload(), call functions in the global namespace they did not expect or they could run into trouble when trying to migrate existing | + | $bar = new bar(); |
+ | bar(); | ||
+ | ?> | ||
+ | </code> | ||
- | ===== Possible approaches ===== | + | Its important that we make a conscious decision for these questions. Depending on how we approach this, users might unintentionally trigger autoload, call functions in the global namespace they did not expect or they could run into trouble when trying to migrate existing code to namespaces. |
+ | One way to avoid this is via an explicit " | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | use \dong as bar; | ||
+ | |||
+ | $bar = new bar(); | ||
+ | \bar(); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | While there is no way to magically import the right things all namespaces, we do have the option of automatically falling back into the global namespace if the identifier does not resolve in the local namespace. This RFC details some alternative approaches for this as well as how things would be like if such a fallback would not exist. | ||
+ | |||
+ | ===== 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 (there will still be cases, like with instanceof/ | ||
- | 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. | + | In the past people often created classes for the sole reason |
- | In the past people created | + | One noteworthy aspect here is that for classes |
- | One noteworthy aspect here is that for classes | + | For functions however |
- | For functions however we do not have __autoload() capabilities. This brings | + | At the same time the ability |
- | At the same time the ability | + | Furthermore users need to be aware that if they are overloading internal |
- | Further more users need to be aware that if they are overloading internal identifiers that they to make sure that either the relevant | + | file1.php |
+ | <code php> | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | function | ||
+ | ?> | ||
+ | </code> | ||
- | One approach to make it at least noticeable when a fallback into the global namespace occurs would be to for example throw an E_NOTICE. This would obviously discourage users from using the fallback for overloading, | + | file2.php |
+ | <code php> | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | // removing the commenting of the following line will change the behavior of file2.php | ||
+ | // include ' | ||
+ | strlen(); | ||
+ | ?> | ||
+ | </ | ||
- | Another approach to reduce some of the issues is by simply removing functions (and constants) from namespaces. | + | 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, |
+ | |||
+ | Another approach to reduce | ||
As a result of the above notes, we might decide to go with a few different options based on how one weighs these aspects: | As a result of the above notes, we might decide to go with a few different options based on how one weighs these aspects: | ||
- | - only for functions/ | + | - Only for functions/ |
- | - only for classes | + | - Only for classes |
- | - only for internal identifiers | + | - Only for internal identifiers |
- | - for everything | + | - For everything |
=== Only for functions/ | === Only for functions/ | ||
Line 50: | Line 87: | ||
Assumption: Most people will use global functions and namespaced classes. | Assumption: Most people will use global functions and namespaced classes. | ||
- | By throwing an E_NOTICE when a fallback occurs, the performance issues become more manageable, but it would reduce the feasibility of overloading. | + | Notes: |
- | + | ||
- | Also note that if functions (and constants) would be removed from namespaces, then most disadvantages would be removed as functions (and constants) would always directly reference the global namespace. | + | |
== Advantages == | == Advantages == | ||
- Does not require fully qualified names for functions (and constants) | - Does not require fully qualified names for functions (and constants) | ||
- | - No performance " | + | - No performance " |
- Ability to overload global functions (and constants) | - Ability to overload global functions (and constants) | ||
Line 64: | Line 99: | ||
- Classes still need fully qualified names | - Classes still need fully qualified names | ||
- | === Only for classes === | + | === Only for classes |
Assumption: People want to overload global classes | Assumption: People want to overload global classes | ||
- | By throwing an E_NOTICE when a fallback occurs, the performance issues become more manageable, but it would reduce the feasibility of overloading. | + | Notes: |
== Advantages == | == Advantages == | ||
Line 76: | Line 111: | ||
== Disadvantages == | == Disadvantages == | ||
- Functions (and constants) still need fully qualified names | - Functions (and constants) still need fully qualified names | ||
- | - Possible performance bomb with __autoload() | + | - Possible performance bomb with autoload |
=== Only for internal identifiers === | === Only for internal identifiers === | ||
Line 95: | Line 130: | ||
Assumption: People want to easily migrate their existing code and beginners should not have to know (as much about) if they are coding inside a namespace or not. | Assumption: People want to easily migrate their existing code and beginners should not have to know (as much about) if they are coding inside a namespace or not. | ||
- | By throwing an E_NOTICE when a fallback occurs, the performance issues become more manageable, but it would reduce the feasibility of overloading. | + | Notes: |
- | + | ||
- | Also note that if functions (and constants) would be removed from namespaces, then some of the disadvantages would be removed as functions (and constants) would always directly reference the global namespace. | + | |
== Advantages == | == Advantages == | ||
Line 106: | Line 139: | ||
== Disadvantages == | == Disadvantages == | ||
- There is overhead for the fallback | - There is overhead for the fallback | ||
- | - Additionally there is a possible performance bomb with __autoload() | + | - Additionally there is a possible performance bomb with autoload |
- Overloading global identifiers requires ensuring that all relevant files are loaded or unexpected behavior might occur | - Overloading global identifiers requires ensuring that all relevant files are loaded or unexpected behavior might occur | ||
- | ==== Require fully qualified names everywhere | + | ==== Do not fall back to the global namespace |
- | Assumption: People are willing to spend more time on updating their legacy code that they migrate to namespaces and adapt their coding when working within namespaces. | + | Assumption: People are willing to spend more time on updating their legacy code that they migrate to namespaces and adapt their coding |
== Advantages == | == Advantages == | ||
- No risk for people relying on behavior that does the same but with more overhead | - No risk for people relying on behavior that does the same but with more overhead | ||
+ | - No risk for ambiguity | ||
== Disadvantages == | == Disadvantages == | ||
- | - Require fully qualified names for all global identifiers | + | - Require fully qualified names (or use statements) |
- | ==== More about RFCs ==== | + | ==== More about namespaces |
- | http://en.wikipedia.org/wiki/Request_for_Comments | + | - http://wiki.php.net/ |
+ | - http://wiki.php.net/ | ||
===== Changelog ===== | ===== Changelog ===== | ||
- | + | - from 1.0 to 1.1: added variant with 1) ns 2) global 3) autoload | |
+ | - from 1.0 to 1.0.1: tweaked examples | ||
+ | - from 0.9 to 1.0: added some examples, added note about use statement, fixed some language issues |
rfc/namespaceresolution.1225363867.txt.gz · Last modified: 2017/09/22 13:28 (external edit)