rfc:case_insensitive_constant_deprecation

Differences

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

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
rfc:case_insensitive_constant_deprecation [2018/06/24 11:00] – created nikicrfc:case_insensitive_constant_deprecation [2018/07/16 16:56] – Accepted nikic
Line 4: Line 4:
   * Target PHP version: PHP 7.3   * Target PHP version: PHP 7.3
   * Implementation: https://github.com/php/php-src/pull/3321   * Implementation: https://github.com/php/php-src/pull/3321
-  * Status: Draft+  * Discussion: https://externals.io/message/102389 
 +  * Status: Accepted
  
 ===== Introduction ===== ===== Introduction =====
Line 12: Line 13:
 The current state of the matter is: The current state of the matter is:
  
-  * Class constants are always case-insensitive.+  * Class constants are always case-sensitive.
   * Global constants declared with ''const'' are always case-sensitive. It should be noted that this applies only to the shortname of the constant, while namespaces in PHP are always case-insensitive.   * Global constants declared with ''const'' are always case-sensitive. It should be noted that this applies only to the shortname of the constant, while namespaces in PHP are always case-insensitive.
   * Constants declared with ''define()'' are case-sensitive by default.   * Constants declared with ''define()'' are case-sensitive by default.
Line 20: Line 21:
  
   * In PHP 7.3: Deprecate calling ''define()'' with third parameter ''true''.   * In PHP 7.3: Deprecate calling ''define()'' with third parameter ''true''.
-  * In PHP 7.3: Deprecate accessing a case-insensitive constant with a casing that differs from the declaration-site.+  * In PHP 7.3: Deprecate accessing a case-insensitive constant with a casing that differs from the declaration-site. The constants ''true'', ''false'' and ''null'' are exempt from this.
   * In PHP 8.0: Remove the possibility of declaring case-insensitive constants.   * In PHP 8.0: Remove the possibility of declaring case-insensitive constants.
-  * The ''true'', ''false'' and ''null'' constants continue to be case-insensitive. The exact way in which they are handled is still up to discussion.+  * In PHP 8.0: ''true'', ''false'' and ''null'' are converted from special-cased constants into reserved keywords
  
 ===== Motivation ===== ===== Motivation =====
Line 70: Line 71:
 Not only was the declaration of a clashing constant permitted, but it effectively changed the value of the ''FOO'' constant. Not only was the declaration of a clashing constant permitted, but it effectively changed the value of the ''FOO'' constant.
  
-This problem is further confounded by assumptions (such as: constants are constant) in the PHP engine and opcache optimizations, resulting in additional issues like [[https://bugs.php.net/bug.php?id=74450|bug #74450]].+This problem is further confounded by assumptions (such as: constants are constant) in the PHP engine and opcache optimizations, resulting in additional issues like [[https://bugs.php.net/bug.php?id=74450|bug #74450]], where the value of a constant changes retroactively.
  
 This is an issue that can in principle be resolved, however it would come with significant additional implementation complexity and a hit to performance and memory usage. At the least, it would require storing lower-cased variants of all constants and checking against them on new constant declarations. This is an issue that can in principle be resolved, however it would come with significant additional implementation complexity and a hit to performance and memory usage. At the least, it would require storing lower-cased variants of all constants and checking against them on new constant declarations.
  
-==== Implementation overhead ====+==== Implementation complexity and overhead ====
  
 Support for case-insensitive constants makes the implementation more complex and slower. Constant lookups are implemented by first looking up the constant name directly, and then looking up a lowercased variant. Support for case-insensitive constants makes the implementation more complex and slower. Constant lookups are implemented by first looking up the constant name directly, and then looking up a lowercased variant.
  
-A particularly extreme case are access to unqualified constants inside namespaces. For example, if constant ''FOO'' is accessed inside namespace ''NS'', the ''FETCH_CONST'' opcode is created with a record-breaking five literals. In order, ''foo\NS'', ''foo\ns'', ''FOO'' and ''foo'' need to be looked up, and finally ''FOO\NS'' is used for error-reporting. For the common case where the intended constant was ''FOO'' this results in three lookups. Thankfully the impact is mitigated by runtime caching (which is actually illegal due to the previous point).+A particularly extreme case are access to unqualified constants inside namespaces. For example, if constant ''FOO'' is accessed inside namespace ''NS'', the ''FETCH_CONST'' opcode is created with a record-breaking five literals. In order, ''ns\FOO'', ''ns\foo'', ''FOO'' and ''foo'' need to be looked up, and finally ''NS\FOO'' is used for error-reporting. For the common case where the intended constant was ''FOO'' this results in three lookups. Thankfully the impact is mitigated by runtime caching (which is actually incorrect due to the previous point).
      
 ===== Proposal ===== ===== Proposal =====
Line 90: Line 91:
 </code> </code>
  
-The ''defined()'' function is not affected. It will continue to return ''true'' for case-sensitive constants, without generating a deprecation warning.+The ''defined()'' function is not affected. It will continue to return ''true'' for case-insensitive constants, without generating a deprecation warning.
  
 Declaration of case-insensitive constants by extensions will not generate a deprecation warning (though their access will). The reason behind this is that the end-user will not be able to do anything about this deprecation warning, while the extension maintainer may not be able to change the declaration for BC reasons at this point. Declaration of case-insensitive constants by extensions will not generate a deprecation warning (though their access will). The reason behind this is that the end-user will not be able to do anything about this deprecation warning, while the extension maintainer may not be able to change the declaration for BC reasons at this point.
Line 100: Line 101:
 ==== Handling of true, false and null ==== ==== Handling of true, false and null ====
  
-''true'', ''false'' and ''null'' in PHP are originally "ordinary" case-insensitive constants, though in practice they are subject to various special casing. For example, these constants are not subject to namespace fallback, as we must be able to resolve their values at compile-time. (The are however subject to aliasing.)+''true'', ''false'' and ''null'' in PHP are originally "ordinary" case-insensitive constants, though in practice they are subject to various special casing. For example, these constants are not subject to namespace fallback, as we must be able to resolve their values at compile-time.
  
-There are at least two ways in which we can handle these special constants:+''true'', ''false'' and ''null'' are the only case-insensitive constants which are commonly used with a casing different from their declaration (which is ''TRUE'', ''FALSE'' and ''NULL''). In the mind of programmers not familiar with the details of the PHP implementation, these resemble keywords more than constants.
  
-The first is to retain them as special case-insensitive constantsGiven all the other special casing they receive, this would not be particularly unusual, though it would require us to introduce or keep special checks for these particular constants.+This RFC proposes to convert ''true'', ''false'' and ''null'' into proper reserved keywords in PHP 8 (reserved keywords are always case-insensitive)This has two implications with regard to backwards compatibility:
  
-The second is to turn them into reserved keywords. This would be the more principled approachbut would also be a slightly larger BC break. It particular it would mean that ''constant("null")'' and similar will no longer work and that ''true'', ''false'' and ''null'' can no longer be used as identifiers. It should be noted though that their use as identifiers is already limited, as they are reserved class names.+  * As ''true''''false'' and ''null'' are no longer constants, they will not be accessible through ''constant("true")'', ''defined("true")'' etc. The names can also no longer be treated as namespaced names, such that ''\true'' and ''namespace\true'' would become invalid. 
 +  * ''true'', ''false'' and ''null'' can no longer be used as identifiers. They will remain legal method and class constant names, as these are not subject to reserved keyword restrictions. It should be noted that these symbols are already forbidden as class names and as (namespaced) global constant names, so effectively this means that only the use as a function name is additionally forbidden.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 112: Line 114:
 Additional deprecation warnings are thrown in PHP 7.3. Case-insensitive constants are removed in PHP 8. Additional deprecation warnings are thrown in PHP 7.3. Case-insensitive constants are removed in PHP 8.
  
-Depending on the choice regarding ''true'', ''false'' and ''null'', they may no longer be accessible as constants and may no longer be used as identifiers.+''true'', ''false'' and ''null'' become reserved keywords in PHP 8. See the end of the previous section for the BC implications this has.
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
  
 Magic constants are not affected. These are already reserved keywords (always case-insensitive), not accessible via ''constant()'', etc. Magic constants are not affected. These are already reserved keywords (always case-insensitive), not accessible via ''constant()'', etc.
 +
 +Class constants are not affected, they are already case-sensitive.
  
 ===== Vote ===== ===== Vote =====
  
-Since this is a language change, a 2/3 majority is required.+Since this is a language change, a 2/3 majority is required. The vote ends 2018-07-16. 
 + 
 +<doodle title="Deprecate (and later remove) case-insensitive constants?" auth="nikic" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
rfc/case_insensitive_constant_deprecation.txt · Last modified: 2018/07/16 17:18 by nikic