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 [2017/12/10 22:03] – Resolve one open question nikic | rfc:namespace_scoped_declares [2017/12/11 13:19] – nikic | ||
---|---|---|---|
Line 17: | 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 69: | 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 '' | ||
Line 78: | Line 77: | ||
If '' | If '' | ||
- | If code using '' | + | If code using '' |
'' | '' | ||
Line 132: | 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 '' | ||
- | ===== Challenges, Disadvantages, | + | ==== Implementation considerations |
- | ==== Technical challenges ==== | + | Declare directives have to be known at compile-time, |
- | The implementation of this feature will be less simple than it may initially sound: | + | ===== Disadvantages ===== |
- | * 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. | + | ==== Reduced explicitness |
- | * 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 151: | 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 163: | 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 179: | Line 181: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | There is no implementation yet. | + | PR: https:// |
rfc/namespace_scoped_declares.txt · Last modified: 2022/01/25 18:22 by ilutov