mirror of
https://github.com/php/pecl-php-operator.git
synced 2026-03-24 07:02:16 +01:00
master
The `$Id$` keywords were used in CVS and Subversion where they can be substituted with filename, last revision number change, last changed date, and last user who changed it. In Git this functionality is different and can be done with Git attribute ident. These need to be defined manually for each file in the `.gitattributes` file and are afterwards replaced with 40-character hexadecimal blob object name which is based only on the particular file contents. This patch simplifies handling of `$Id$` keywords by removing them since they are not used anymore.
Operator overloading extension for PHP7
Usage
Implement magic methods in a class to provide operator overloading automatically, for example:
class C {
private $value = 0;
public function __add($delta) {
$this->value += $delta;
return $this;
}
public function __toString() {
return strval($this->value);
}
}
$c = new C;
var_dump($c + 5); // object(C)#1 { ["C:value:private"]=>5 }
The following overload methods are supported:
| Operator | Method |
|---|---|
| $o + $arg | __add($arg) |
| $o - $arg | __sub($arg) |
| $o * $arg | __mul($arg) |
| $o / $arg | __div($arg) |
| $o % $arg | __mod($arg) |
| $o ** $arg | __pow($arg) |
| $o << $arg | __sl($arg) |
| $o >> $arg | __sr($arg) |
| $o . $arg | __concat($arg) |
| $o | $arg | __bw_or($arg) |
| $o & $arg | __bw_and($arg) |
| $o ^ $arg | __bw_xor($arg) |
| ~$o | __bw_not() |
| $o === $arg | __is_identical($arg) |
| $o !== $arg | __is_not_identical($arg) |
| $o == $arg | __is_equal($arg) |
| $o != $arg | __is_not_equal($arg) |
| $o < $arg | __is_smaller($arg) |
| $o <= $arg | __is_smaller_or_equal($arg) |
| $o > $arg | __is_greater($arg) * |
| $o >= $arg | __is_greater_or_equal($arg) * |
| $o <=> $arg | __cmp($arg) |
| ++$o | __pre_inc() |
| $o++ | __post_inc() |
| --$o | __pre_dec() |
| $o-- | __post_dec() |
| $o = $arg | __assign($arg) |
| $o += $arg | __assign_add($arg) |
| $o -= $arg | __assign_sub($arg) |
| $o *= $arg | __assign_mul($arg) |
| $o /= $arg | __assign_div($arg) |
| $o %= $arg | __assign_mod($arg) |
| $o **= $arg | __assign_pow($arg) |
| $o <<= $arg | __assign_sl($arg) |
| $o >>= $arg | __assign_sr($arg) |
| $o .= $arg | __assign_concat($arg) |
| $o |= $arg | __assign_bw_or($arg) |
| $o &= $arg | __assign_bw_and($arg) |
| $o ^= $arg | __assign_bw_xor($arg) |
* - __is_greater() and __is_greater_or_equal() require a rebuild of the main PHP runtime using the included patch. Withtout this patch, $a > $b is automatically remapped to $b < $a by the engine.
Description
⚠️ ARCHIVED: Original GitHub repository no longer exists. Preserved as backup on 2026-01-22T16:23:50.865Z
Languages
C
49.6%
PHP
46.7%
M4
2.1%
JavaScript
1.6%