23

PHP: rfc:arithmetic_operator_type_checks

 4 years ago
source link: https://wiki.php.net/rfc/arithmetic_operator_type_checks
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP RFC: Stricter type checks for arithmetic/bitwise operators

Introduction

This RFC proposes to throw a TypeError when an arithmetic or bitwise operator is applied to an array, resource or (non-overloaded) object. The behavior of scalar operands (like null) remains unchanged

I think we can all agree that this is not reasonable behavior:

var_dump([] % [42]);
// int(0)
// WTF?

Let's fix it.

Proposal

Current Behavior

This section describes the current behavior, limited only to the cases where one operand is an array, resource or object. The object cases always assume that no operator or cast overloading is involved. If operator/cast overloading is used, then those overloads apply.

Operators +, -, *, /, **:

  • Throw Error exception on array operand. (Excluding + if both operands are array.)
  • Silently convert a resource operand to the resource ID as an integer.
  • Convert an object operand to integer one, while throwing a notice.

Operators %, <<, >>, &, |, ^:

  • Silently convert an array operand to integer zero if empty or integer one if non-empty.
  • Silently convert a resource operand to the resource ID as an integer.
  • Convert an object operand to integer one, while throwing a notice.

Operator ~:

  • Throw an Error exception for array, resource and object operands.

Operators ++ and --:

  • Silently do nothing if the operand is an array, resource or object.

Proposed Behavior

The proposed behavior is the same for all the arithmetic/bitwise operators +, -, *, /, **, %, <<, >>, &, |, ^, ~, ++, --:

  • Throw a TypeError exception for array, resource and object operands.

Of course, the case of addition with two array operands remains legal.

Unchanged Behavior

The behavior of operands of type null, bool, int, float and string remains the same.

While it is questionable whether true / 17 really is a sensible operation, the handling of scalar values in general can likely not be changed unconditionally, and as such is left to a proposal like the strict operators directive.

The changes proposed here are intended to be entirely uncontroversial.

Backward Incompatible Changes

Using an array, resource or object in an arithmetic/bitwise operation will now consistently throw, while it previously produced a non-sensical value.

Future Scope

In the future, we may wish to go one step further:

  • Make non-numeric string operands throwing. Non-numeric here means not starting with a digit (optionally preceded by whitespace). This would not apply to operators that have special behavior for strings (string increment and bitwise and/or/xor).
  • Make overflowing float values throwing for operators that expect an integer (%, <<, >>, &, |, ^).

This would have the advantage of aligning the semantics with parameter type checks in coercive mode, for the types int and int|float depending on operator. The only discrepancy would be in the handling of null, which is already not as strictly enforced.

I'm leaving this potential improvement out of this RFC, because it requires more consideration regarding backwards compatibility and overall language integration.

Voting started 2020-04-16 and ends 2020-04-30.

Add stricter type checks for arithmetic/bitwise operators?
Real name yes no
ajf (ajf) success.png 
alcaeus (alcaeus) success.png 
asgrim (asgrim) success.png 
ashnazg (ashnazg) success.png 
brzuchal (brzuchal) success.png 
bwoebi (bwoebi) success.png 
carusogabriel (carusogabriel) success.png 
cmb (cmb) success.png 
colinodell (colinodell) success.png 
daverandom (daverandom) success.png 
derick (derick) success.png 
dmitry (dmitry) success.png 
duncan3dc (duncan3dc) success.png 
duodraco (duodraco) success.png 
ekin (ekin) success.png 
galvao (galvao) success.png 
gasolwu (gasolwu) success.png 
girgias (girgias) success.png 
jasny (jasny) success.png 
jbnahan (jbnahan) success.png 
jhdxr (jhdxr) success.png 
jmcastagnetto (jmcastagnetto) success.png 
kelunik (kelunik) success.png 
kguest (kguest) success.png 
klaussilveira (klaussilveira) success.png 
kocsismate (kocsismate) success.png 
lcobucci (lcobucci) success.png 
malukenho (malukenho) success.png 
marandall (marandall) success.png 
marcio (marcio) success.png 
mariano (mariano) success.png 
mbeccati (mbeccati) success.png 
mcmic (mcmic) success.png 
mike (mike) success.png 
narf (narf) success.png 
nicolasgrekas (nicolasgrekas) success.png 
nikic (nikic) success.png 
ocramius (ocramius) success.png 
petk (petk) success.png 
pmmaga (pmmaga) success.png 
pollita (pollita) success.png 
rasmus (rasmus) success.png 
reywob (reywob) success.png 
royopa (royopa) success.png 
ruudboon (ruudboon) success.png 
salathe (salathe) success.png 
sammyk (sammyk) success.png 
sebastian (sebastian) success.png 
sergey (sergey) success.png 
sirsnyder (sirsnyder) success.png 
stas (stas) success.png 
svpernova09 (svpernova09) success.png 
tandre (tandre) success.png 
trowski (trowski) success.png 
wyrihaximus (wyrihaximus) success.png 
yunosh (yunosh) success.png 
zimt (zimt) success.png 
Final result: 57 0
This poll has been closed.
rfc/arithmetic_operator_type_checks.txt · Last modified: 2020/05/05 14:12 by nikic

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK