Skip to content
Operator overloading for Objects
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests Fix some minor whitespace issues Oct 4, 2018
.gitignore PHP7 rewrite Mar 9, 2017
CREDITS Fix some minor whitespace issues Oct 4, 2018
EXPERIMENTAL Initial Commit Jan 12, 2006
README.md
config.m4 Remove unused Git attributes ident Oct 4, 2018
config.w32 Remove unused Git attributes ident Oct 4, 2018
operator.c Fix some minor whitespace issues Oct 4, 2018
package.xml Fix some minor whitespace issues Oct 4, 2018
php7-is_greater.diff Add support for is_greater(_or_equal) back Mar 9, 2017

README.md

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.

You can’t perform that action at this time.