rfc:namespace_scoped_declares
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:namespace_scoped_declares [2016/09/22 20:52] – nikic | rfc:namespace_scoped_declares [2017/12/11 13:19] – nikic | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2016-09-20 | * Date: 2016-09-20 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Proposed PHP version: PHP 7.2 | + | * Proposed PHP version: PHP 7.3 |
- | * Status: | + | * Status: |
+ | * PR: https:// | ||
+ | * ML thread: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
Line 15: | Line 17: | ||
// bootstrap.php | // bootstrap.php | ||
namespace_declare(' | namespace_declare(' | ||
- | namespace_declare(' | ||
</ | </ | ||
- | The first call would make '' | + | This would make '' |
===== Motivation and Vision ===== | ===== Motivation and Vision ===== | ||
Line 37: | Line 38: | ||
</ | </ | ||
- | For libraries using Composer this bootstrap file would be specified as an autoloader | + | For libraries using Composer this bootstrap file would be specified as a " |
+ | |||
+ | <code json> | ||
+ | { | ||
+ | | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | However, if this kind of approach should prove popular, it might be possible to include this information directly in the '' | ||
<code json> | <code json> | ||
Line 55: | Line 68: | ||
<code php> | <code php> | ||
- | function namespace_declare(string $namespace, array $directives) : void; | + | function namespace_declare(string $namespace, array $declares) : void; |
</ | </ | ||
- | The given '' | + | The given '' |
- | If the provided '' | + | If the provided '' |
If '' | If '' | ||
- | The declare directive defaults specified by '' | + | If code using '' |
'' | '' | ||
Line 118: | Line 131: | ||
If we go for throwing a warning here, it might be beneficial to add a '' | If we go for throwing a warning here, it might be beneficial to add a '' | ||
- | ==== Open Question: namespace_declare after namespace already used ==== | + | ==== Implementation considerations |
- | As specified above, it is possible | + | Declare directives have to be known at compile-time, |
- | To avoid this, '' | + | ===== Disadvantages ===== |
- | ===== Challenges, Disadvantages, | + | ==== Reduced explicitness |
- | + | ||
- | ==== Technical challenges ==== | + | |
- | + | ||
- | The implementation of this feature will be less simple than it may initially sound: | + | |
- | + | ||
- | * Namespace-scoped declares will have to be taken into account by opcache. Namely, if a file is compiled with a certain set of namespace-scoped declares, it cannot necessarily be reused if it is compiled with a different set of declares. This could be solved by storing a checksum based on the namespace-scoped declares at the time of compilation together with the cached file and compare it when loading it. I believe this can be done efficiently. | + | |
- | * If the open question " | + | |
- | + | ||
- | ==== Disadvantages ==== | + | |
- | + | ||
- | === Reduced explicitness === | + | |
It could be argued that the introducing of namespace-scoped declares will make it less discoverable which declare values are active inside a given file, because they are no longer required to be specified at the top of the file. | It could be argued that the introducing of namespace-scoped declares will make it less discoverable which declare values are active inside a given file, because they are no longer required to be specified at the top of the file. | ||
Line 143: | Line 145: | ||
Additionally it should be noted that the same problem (to an even worse degree) exists for the ini system, where ini options can be specified from many different sources (including multiple ini files, htaccess files and inline in PHP code). In practice this does not appear to be a major problem. | Additionally it should be noted that the same problem (to an even worse degree) exists for the ini system, where ini options can be specified from many different sources (including multiple ini files, htaccess files and inline in PHP code). In practice this does not appear to be a major problem. | ||
- | === Proliferation of declare directives === | + | Finally, while namespace-scoped declares may be less explicit, they fit better into the mental model we use as programmers. Libraries generally do not haphazardly switch between different strict_types files and instead use a consistent mode for the entire project. Namespace-scoped declares codify this and prevent mistakes like forgetting to add a declare directive when creating a new file. |
+ | |||
+ | ==== Proliferation of declare directives | ||
While this RFC proposes specific functionality (namespace-scoped declares), the Motivation section outlined a likely future application of this mechanism, which is the introduction of additional declare directives that modify language behavior to be " | While this RFC proposes specific functionality (namespace-scoped declares), the Motivation section outlined a likely future application of this mechanism, which is the introduction of additional declare directives that modify language behavior to be " | ||
Line 155: | Line 159: | ||
Nonetheless, | Nonetheless, | ||
- | ==== Alternatives ==== | + | ==== Discussion: Potential for abuse ==== |
+ | |||
+ | One issue that came up repeatedly during the first discussion, is that people perceived a potential for abuse in this feature. The concern is that it would be possible to call '' | ||
+ | |||
+ | I honestly do not understand this concern. PHP is already oversaturated with ways in which you could break external library code if one wants to (such as hijacking autoloading). If breaking library code is your goal, you don't need this feature to achieve that. However, the question remains why anyone would want to this, as in the end you only sabotage yourself. | ||
+ | |||
+ | ===== Alternatives | ||
The main alternative to this proposal I see is the introduction of a " | The main alternative to this proposal I see is the introduction of a " | ||
Line 171: | Line 181: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | There is no implementation yet. | + | PR: https:// |
+ | |||
+ | |||
+ | ===== TODOs ===== | ||
+ | |||
+ | * Resolve open question " | ||
+ | * Consider adding some introspection functionality, | ||
+ | * Investigate whether this mechanism can support the '' |
rfc/namespace_scoped_declares.txt · Last modified: 2022/01/25 18:22 by ilutov