rfc:namespace_scoped_declares

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:namespace_scoped_declares [2017/12/11 12:54] nikicrfc:namespace_scoped_declares [2017/12/11 13:19] nikic
Line 134: Line 134:
  
 Declare directives have to be known at compile-time, because they may directly influence the emitted opcodes. Files will always be compiled for the current declare configuration and opcache will cache them for that specific configuration. Opcache additionally stores which namespaces and namespace-scoped declares are used and will verify that the (relevant) namespace-scoped declares did not change when the file is loaded again. If the (relevant) namespace-scoped declares changed, the file will be invalidated and compiled again for the new configuration. Declare directives have to be known at compile-time, because they may directly influence the emitted opcodes. Files will always be compiled for the current declare configuration and opcache will cache them for that specific configuration. Opcache additionally stores which namespaces and namespace-scoped declares are used and will verify that the (relevant) namespace-scoped declares did not change when the file is loaded again. If the (relevant) namespace-scoped declares changed, the file will be invalidated and compiled again for the new configuration.
- 
  
 ===== Disadvantages ===== ===== Disadvantages =====
Line 145: Line 144:
  
 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.
 +
 +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 ==== ==== Proliferation of declare directives ====
Line 157: Line 158:
  
 Nonetheless, I think that proliferation of declare directives is a real danger and we need to ensure that new directives are not added frivolously. Nonetheless, I think that proliferation of declare directives is a real danger and we need to ensure that new directives are not added frivolously.
 +
 +==== 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 ''namespace_declare()'' on a namespace one doesn't own, and consequently break code using that namespace.
 +
 +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 ===== ===== Alternatives =====
rfc/namespace_scoped_declares.txt · Last modified: 2022/01/25 18:22 by ilutov