Add boolean assignment operators
Description
Currently there are no ||= &&= operators. A bitwise &= |= do exist, but will change the type of the operand.
Use case in pseudo
function test(array $a): bool
{
$result = true;
foreach($a as $element) {
$result &= $element-> test();
}
return $result;
}
See https://github.com/php/php-src/pull/4535 for one of the issues regarding these operators. I don't see a clear benefit to have these operators (and apparently, most other languages which have the logical operators and combined assignment operators do not either), so I strongly suggest to pursue the RFC process.
Well here is one : C#
Well here is one : C#
Nope. Note: The conditional logical operators && and || don't support compound assignment.
However, JS added that in ES2021 (w/ short-circuiting): https://github.com/tc39/proposal-logical-assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment https://2ality.com/2020/06/logical-assignment-operators.html
I don't see a clear benefit to have these operators (and apparently, most other languages which have the logical operators and combined assignment operators do not either)
@cmb69 Beside shorter syntax, there are 2 more "benefits" from using a ||= b instead of a = a || b:
1. Setter is only called when not short-circuiting, this is similar to a || (a = b):
a || (a = b):new class
{
private array $data = ['foo' => true, 'bar' => false,];
function __construct()
{
$this->foo ||= true;
assert($this->data['foo'] === true);
$this->bar ||= false;
assert($this->data['bar'] === false);
}
function __get(string $name): bool
{
echo "Getter called for {$name}\n";
return $this->data[$name];
}
function __set(string $name, bool $value): void
{
echo "Setter called for {$name}\n";
$this->data[$name] = $value;
}
};
should be expected:
Getter called for foo
Getter called for bar
Setter called for bar
https://3v4l.org/LJp09
Edit: 1. is not playing well with PHP
- it's easier to use with incrementing keys:
$array = [false, false, false];
$key = 0;
$array[$key++] ||= true;
$array[++$key] ||= true;
assert($array === [true, false, true]);
https://3v4l.org/FGBjj
@KapitanOczywisty, that is actually what I find most confusing. Consider $a = 0; $a &&= $b. Would that result in $a === false? Analogous for $a = "foo"; $a ||= $b.
Probably should always convert to bool if not already. First feature is not that important for PHP, in JS setters are more common and || doesn't change type.
The code above is sub-optimal anyway, since the loop should stop once $result is false.
function test(array $a): bool
{
foreach($a as $element) {
if (!$element-> test()) return false;
}
return true;
}
I find the coercion that @cmb69 mentioned confusing too.
A trivial example does have a trivial optimization. If test was doThing that had side effects and all methods need to be called then you can't optimize it away.
@KapitanOczywisty, that is actually what I find most confusing. Consider $a = 0; $a &&= $b. Would that result in $a === false? Analogous for $a = "foo"; $a ||= $b.
I'm going to have to think about this a bit but generally I don't know that any behavior is really confusing if you consider this to be a condensed version of $a = $a && $b and $a = $a || $b like other assignment operators. Setting aside how coercion can generally confuse things, the behavior for this seems well defined unless I'm missing part of your question.
What isn't immediately clear to me is
- would be affected by strict types? are other assignment operators?
- the discussion around short circuiting. Is that relevant, and what would it mean to PHP.
There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.