rfc:notice-for-non-valid-array-container

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:notice-for-non-valid-array-container [2016/08/10 22:40] bp1222rfc:notice-for-non-valid-array-container [2019/07/10 12:20] (current) – Implemented nikic
Line 1: Line 1:
 ====== PHP RFC: E_WARNING for invalid container read array-access ====== ====== PHP RFC: E_WARNING for invalid container read array-access ======
-  * Version: 0.2 +  * Version: 1.0 
-  * Date: 2016-08-10+  * Date: 2016-08-16
   * Author: David Walker (dave@mudsite.com)   * Author: David Walker (dave@mudsite.com)
-  * Status: Under Discusson+  * Status: Implemented (in PHP 7.4)
   * First Published at: http://wiki.php.net/rfc/notice-for-non-valid-array-container   * First Published at: http://wiki.php.net/rfc/notice-for-non-valid-array-container
  
Line 27: Line 27:
                      // Output would be:                      // Output would be:
                      // Warning: Variable of type integer does not accept array offsets                      // Warning: Variable of type integer does not accept array offsets
 +                     
 +// Brought up during vote:
 +$a = [null];
 +$c = null;
 +var_dump($a[0][0] + $c[0]);  // This will through 2 E_WARNINGS, First would be $a[0][0]
 +                             // For the same reason as above, $a[0] is rightfully NULL
 +                             // and accessing [0] on null is invalid.  The second, 
 +                             // would be $c[0] for the same reason.
 +                             // Output:
 +                             // int(0)
 </file> </file>
  
Line 35: Line 45:
  
 This RFC aims to limit the quantity of warnings on a single line, however, large projects may have many locations that might need variable type checking around unknown container access.  Typically, one might assume access of a variable to be an array or object, and could do checks to make sure the variable about to be accessed is an array, or an object. This RFC aims to limit the quantity of warnings on a single line, however, large projects may have many locations that might need variable type checking around unknown container access.  Typically, one might assume access of a variable to be an array or object, and could do checks to make sure the variable about to be accessed is an array, or an object.
 +
 +===== Performance Impact =====
 +Test run was:
 +<file php>
 +<?php
 +$a = false;
 +for ($i = 0; $i < 1000000; $i++) {
 +    $a[0];
 +}
 +</file>
 +
 +Execution Time (DualCore 3ghz; 2g ram)
 +  * Current Master : ~0.09s (~489m operations)
 +  * Current Master w/RFC Displaying Warnings: ~33.25s (~7.799b operations)
 +  * Current Master w/RFC Hiding Warnings: ~0.82s (~4.091b operations)
 +
 +We can see there is a significant increase in operations on huge loads.  However, I wouldn't suspect 1m of these errors per request almost ever.  So, yes there is; but I'd call it useful information overhead.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 53: Line 80:
  
 ===== Discussed Issues ===== ===== Discussed Issues =====
 +==== E_NOTICE or E_WARNING ====
 +I began this RFC with the implementation raising an E_NOTICE.  However, when attempting to access a scalar value as array for a [[https://github.com/bp1222/php-src/blob/7369cfcc0f215156eafae71e2b62a573512b3d05/Zend/zend_execute.c#L1776|write-context]] it raises an E_WARNING.  So I'm mimicking this behavior when accessing any scalar not just those <= IS_FALSE on a read-context.
 +
 ==== NULL Identity ==== ==== NULL Identity ====
 Should NULL variables be treated as a special identity so that accessing array on a null just returns null.   Should NULL variables be treated as a special identity so that accessing array on a null just returns null.  
Line 63: Line 93:
   * ''foreach(list($a, $b) = each($foo))'' -- Would need to prevent warn on 'final' null each   * ''foreach(list($a, $b) = each($foo))'' -- Would need to prevent warn on 'final' null each
  
-Per discussion on the [[https://github.com/php/php-src/pull/2031#issuecomment-238366706|PR]] I have limited this RFC to not raise warings when setting any value by use of list().+Per discussion on the [[https://github.com/php/php-src/pull/2031#issuecomment-238366706|PR]] I have limited this RFC to not raise warnings when setting any value by use of list().
  
 ==== Reference Assignment Access ==== ==== Reference Assignment Access ====
Line 71: Line 101:
 $value = null; $value = null;
 $dim = 0; $dim = 0;
-$new_val =& $null[$dim]; +$new_val =& $value[$dim]; 
-var_dump($null);+var_dump($value);
 /* /*
    Output:    Output:
Line 82: Line 112:
 </file> </file>
  
-In the above example the variable ''$null'' is accessed via write, and current operations there instruct the variable to become an array, where $dim is null, and then create that as a reference.  I dislike how this is unique to NULL values being accessed, wherein bools/floats/ints silently fail, and the return value is undefined.  Regardless, because this is accessed as a write, it falls outside the scope of this RFC which aims to raise a warning for read access.+In the above example the variable ''$null'' is accessed via write, and current operations there instruct the variable to become an array, where $value[$dimis created as null, and then make it a reference.  I dislike how this is unique to NULL values being accessed, wherein bools/floats/ints silently fail, and the return value is undefined.  Regardless, because this is accessed as a write, it falls outside the scope of this RFC which aims to raise a warning for read access.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-No syntax is changed, a vote of 50%+1 will be necessary+Requires 2/3 Vote 
 +<doodle title="E_WARNING for invalid container read array-access" auth="bp1222" voteType="single" closed="true"> 
 +   * Yes 
 +   No 
 +</doodle> 
 +Vote Start: 2016-08-16 15:36 
 + 
 +Vote End:   2016-08-31 23:59
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/notice-for-non-valid-array-container.1470868801.txt.gz · Last modified: 2017/09/22 13:28 (external edit)