rfc:namespaceresolution
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | rfc:namespaceresolution [2011/04/06 10:59] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Non fully qualified namespaced identifier resolution RFCs ====== | ||
+ | * Version: 1.1.0 | ||
+ | * Date: 2008-10-30 | ||
+ | * Author: Lukas Smith < | ||
+ | * Status: Accepted | ||
+ | * First Published at: http:// | ||
+ | This RFC discusses the way identifiers inside a namespace are to be resolved that are not fully qualified. | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | Generally in namespaces we support fully qualified names as well as importing namespaces via " | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | |||
+ | $bar = new bar(); | ||
+ | bar(); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | 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 ==== | ||
+ | |||
+ | 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/ | ||
+ | |||
+ | In the past people often created classes for the sole reason of being able to sort of " | ||
+ | |||
+ | One noteworthy aspect here is that for classes we have autoload. If non fully qualified identifiers can be used to reference global identifiers, | ||
+ | |||
+ | 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). | ||
+ | |||
+ | Furthermore users need to be aware that if they are overloading internal identifiers that they need to make sure that the relevant code is loaded in time. For classes there is the autoload approach which would help ensure that the class to overload is loaded on demand if necessary. However users that do not use autoload or that are overloading function (and constants) run the risk of their code behaving differently in not so obvious ways if they do not always load all files defining relevant functions (and constants) for this namespace. | ||
+ | |||
+ | file1.php | ||
+ | <code php> | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | function strlen(){} | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | file2.php | ||
+ | <code php> | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | // removing the commenting of the following line will change the behavior of file2.php | ||
+ | // include ' | ||
+ | strlen(); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | 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 (though it does not remove the issues entirely) some of the issues is by simply removing functions (and constants) from namespaces. | ||
+ | |||
+ | 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 classes | ||
+ | - Only for internal identifiers | ||
+ | - For everything | ||
+ | |||
+ | === Only for functions/ | ||
+ | |||
+ | Assumption: Most people will use global functions and namespaced classes. | ||
+ | |||
+ | Notes: By throwing an E_NOTICE when a fallback occurs, the performance issues become more manageable, but it would reduce the feasibility of overloading. 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 == | ||
+ | - Does not require fully qualified names for functions (and constants) | ||
+ | - No performance " | ||
+ | - Ability to overload global functions (and constants) | ||
+ | |||
+ | == Disadvantages == | ||
+ | - Overloading global identifiers requires ensuring that all relevant files are loaded or unexpected behavior might occur | ||
+ | - There is still overhead for the fallback | ||
+ | - Classes still need fully qualified names | ||
+ | |||
+ | === Only for classes (autoload first) === | ||
+ | |||
+ | Assumption: People want to overload global classes | ||
+ | |||
+ | Notes: By throwing an E_NOTICE when a fallback occurs, the performance issues become more manageable, but it would reduce the feasibility of overloading. Also if instead of checking autoload before global, one could first check global before falling back to autoload. This prevents performance issues, but would raise issues with the load order similar to functions/ | ||
+ | |||
+ | == Advantages == | ||
+ | - Does not require fully qualified names for classes | ||
+ | - Ability to overload global classes | ||
+ | |||
+ | == Disadvantages == | ||
+ | - Functions (and constants) still need fully qualified names | ||
+ | - Possible performance bomb with autoload | ||
+ | |||
+ | === Only for internal identifiers === | ||
+ | |||
+ | Assumption: People will leave the global namespace to PHP and namespace their own code. | ||
+ | |||
+ | == Advantages == | ||
+ | - Does not require fully qualified names for all internal identifiers | ||
+ | - Internal identifiers work the same inside and outside of namespaces (though overloading would still be possible) | ||
+ | |||
+ | == Disadvantages == | ||
+ | - Less clear rule as its not possible form just reading the calling code if something is internal or not | ||
+ | - Defining a function in userland code to emulate functionality from a newer PHP version will not enable fallbacks | ||
+ | - Higher performance overhead (???) | ||
+ | |||
+ | === For everything === | ||
+ | |||
+ | 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. | ||
+ | |||
+ | Notes: By throwing an E_NOTICE when a fallback occurs, the performance issues become more manageable, but it would reduce the feasibility of overloading. 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. Also if instead of checking autoload before global, one could first check global before falling back to autoload. This prevents performance issues for classes, but would raise issues with the load order similar to functions/ | ||
+ | |||
+ | == Advantages == | ||
+ | - Does not require fully qualified names for all global identifiers | ||
+ | - Simple rule, everything falls back | ||
+ | - Ability to overload any kind of global identifier | ||
+ | |||
+ | == Disadvantages == | ||
+ | - There is overhead for the fallback | ||
+ | - 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 | ||
+ | |||
+ | ==== 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 style to fully qualify global identifiers when working within namespaces. | ||
+ | |||
+ | == Advantages == | ||
+ | - No risk for people relying on behavior that does the same but with more overhead | ||
+ | - No risk for ambiguity | ||
+ | |||
+ | == Disadvantages == | ||
+ | - Require fully qualified names (or use statements) for all global identifiers | ||
+ | |||
+ | ==== More about namespaces ==== | ||
+ | |||
+ | - http:// | ||
+ | - http:// | ||
+ | |||
+ | ===== 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.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1