rfc:autovivification_false

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:autovivification_false [2021/05/25 17:14] – Explained error messages dharmanrfc:autovivification_false [2021/07/20 16:42] (current) – Implemented dharman
Line 1: Line 1:
-====== PHP RFC: Disable autovivification on false ======+====== PHP RFC: Deprecate autovivification on false ======
   * Date: 2021-05-25   * Date: 2021-05-25
   * Author: Kamil Tekiela <dharman@php.net>   * Author: Kamil Tekiela <dharman@php.net>
-  * Status: Under Discussion +  * Status: Implemented in PHP 8.1 
-  * Target VersionPHP 8.1+  * Implementationhttps://github.com/php/php-src/pull/7131
  
 ===== Introduction ===== ===== Introduction =====
  
-PHP natively allows for [[https://en.wikipedia.org/wiki/Autovivification|autovivification]] (auto-creation of arrays from falsey values). This feature is very useful and used in a lot of PHP projects, especially if the variable is undefined or has been nullified before. However, there is a little oddity that allows creating an array from a false value. +PHP natively allows for [[https://en.wikipedia.org/wiki/Autovivification|autovivification]] (auto-creation of arrays from falsey values). This feature is very useful and used in a lot of PHP projects, especially if the variable is undefined. However, there is a little oddity that allows creating an array from a false and null value. 
  
 <PHP> <PHP>
 +// From undefined
 +$arr[] = 'some value';
 +$arr['doesNotExist'][] = 2;
 +// From false
 $arr = false; $arr = false;
 +$arr[] = 2;
 +// From null
 +$arr = null;
 $arr[] = 2; $arr[] = 2;
 </PHP> </PHP>
Line 18: Line 25:
 ===== Proposal ===== ===== Proposal =====
  
-The proposal is to disallow autovivification from %%false%% values. We will continue to allow autovivification from undefined variables or null values, but in every other case an error message will be shown:+The proposal is to disallow autovivification from %%false%% values. We will continue to allow autovivification from undefined variable and %%null%%, but in every other case an error message will be shown:
  
 > Cannot use a scalar value as an array > Cannot use a scalar value as an array
 +
 +In PHP 8.1, appending to a variable of type false will display a deprecation notice. For example:
 +
 +> Deprecated: Automatic conversion of false to array is deprecated in
 +
 +In PHP 9.0, this will throw a fatal error, the same as for other scalar types. 
  
 ===== Rationale ===== ===== Rationale =====
Line 33: Line 46:
 </PHP> </PHP>
  
-Throwing an error in such cases could save developers from common bugs in their code. On the other hand, this could also be considered useful by some developers. The rationale here is that the behaviour is inconsistent. Neither true, 0, nor "" can be used in autovivification today, so does it make sense to allow false?+Throwing an error in such cases could save developers from common bugs in their code. On the other hand, this feature could also be considered useful by some developers. The rationale here is that the behaviour is inconsistent. Neither true, 0, nor "" can be used in autovivification today, so does it make sense to allow false? 
 + 
 +Allowing autovivification on %%null%% can be a sign of a buggy code, but it is less of a problem than %%false%%. In PHP, %%null%% and undefined are very similar and often treated the same way, e.g. %%isset()%% and %%??%%.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-Every single usage autovivification on false values will throw deprecation error in PHP 8.1 and throw a fatal error in PHP 9.0. +Every single usage autovivification on false will throw deprecation error in PHP 8.1 and throw a fatal error in PHP 9.0.
  
 ===== Vote ===== ===== Vote =====
  
-Yes/No.+Should we deprecate autovivification from false? 
 + 
 +Started: 2021-06-09T20:00:00Z\\ 
 +Ends: 2021-06-23T20:00:00Z 
 + 
 +<doodle title="Deprecate autovivification from false?" auth="dharman" voteType="single" closed="true">  
 +   Yes 
 +   No 
 +</doodle> 
 + 
 +===== Discussion on internals ====== 
 + 
 +https://externals.io/message/114595 
rfc/autovivification_false.1621962842.txt.gz · Last modified: 2021/05/25 17:14 by dharman