PHPackages                             jblo/operator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jblo/operator

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

jblo/operator
=============

PHP extension for operator overloading

30PHP

Since Jun 22Pushed today2 watchersCompare

[ Source](https://github.com/jb-lopez/php-operators)[ Packagist](https://packagist.org/packages/jblo/operator)[ RSS](/packages/jblo-operator/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

PHP Operator Overloading
========================

[](#php-operator-overloading)

This extension provides operator overloading for PHP 8.

There is an extension for operator overloading in PHP 7, [pecl-php-operator](https://github.com/php/pecl-php-operator), but it has been broken since PHP 7.4. This extension is a rewrite of that extension, but for PHP 8.

Regular Operators
-----------------

[](#regular-operators)

These operators are the ones that make the most sense to overload. They are assignment, unary, binary, and comparison operators.

### [Arithmetic Operators](https://www.php.net/manual/en/language.operators.arithmetic.php)

[](#arithmetic-operators)

Operator NameOperatorImplementedFunction CallIdentity`+$a`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)Negation`-$a`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)Addition`$a + $b`Yes`$a::__add($b)`Subtraction`$a - $b`Yes`$a::__sub($b)`Multiplication`$a * $b`Yes`$a::__mul($b)`Division`$a / $b`Yes`$a::__div($b)`Modulo`$a % $b`Yes`$a::__mod($b)`Exponent`$a ** $b`Yes`$a::__pow($b)`### [Assignment Operators](https://www.php.net/manual/en/language.operators.assignment.php)

[](#assignment-operators)

Operator NameOperatorImplementedFunction CallAssignment`$a = $b`Yes`$a::__assign($b)`### [Bitwise Operators](https://www.php.net/manual/en/language.operators.bitwise.php)

[](#bitwise-operators)

Operator NameOperator UseImplementedFunction CallAnd`$a & $b`Yes`$a::__bw_and($b)`Or`$a | $b`Yes`$a::__bw_or($b)`Xor`$a ^ $b`Yes`$a::__bw_xor($b)`Not`~$a`Yes`$a::__bw_not($b)`Shift left`$a > $b`Yes`$a::__sr($b)`### [Comparison Operators](https://www.php.net/manual/en/language.operators.comparison.php)

[](#comparison-operators)

Operator NameOperator UseImplementedFunction CallEqual`$a == $b`Yes`$a::__is_equal($b)`Identical`$a === $b`Yes`$a::__is_identical($b)`Not equal`$a != $b`Yes`$a::__is_not_equal($b)`Not equal`$a  $b`Yes`$a::__is_not_equal($b)`Not identical`$a !== $b`Yes`$a::__is_not_identical($b)`Less than`$a < $b`Yes`$a::__is_smaller($b)`Greater than`$a > $b`Yes[2](#user-content-fn-4-d6399168c96e5415e25a1281eb032679)`$a::__is_greater($b)`Less than or equal`$a = $b`Yes[2](#user-content-fn-4-d6399168c96e5415e25a1281eb032679)`$a::__is_greater_or_equal($b)`Spaceship`$a  $b`Yes`$a::__spaceship($b)`Ternary`$a ?: $b`NoNull coalescing`$a ?? $b`No[3](#user-content-fn-3-d6399168c96e5415e25a1281eb032679)### [Incrementing/Decrementing Operators](https://www.php.net/manual/en/language.operators.increment.php)

[](#incrementingdecrementing-operators)

Operator NameOperator UseImplementedFunction CallPre-increment`++$a`Yes`$a::__pre_inc`Post-increment`$a++`Yes`$a::__post_inc`Pre-decrement`--$a`Yes`$a::__pre_dec`Post-decrement`$a--`Yes`$a::__post_dec`### [Logical Operators](https://www.php.net/manual/en/language.operators.logical.php)

[](#logical-operators)

Operator NameOperator UseImplementedFunction CallAnd`$a and $b`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)Or`$a or $b`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)Xor`$a xor $b`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)Not`$a ! $b`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)And`$a && $b`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)Or`$a || $b`Maybe[1](#user-content-fn-1-d6399168c96e5415e25a1281eb032679)### [String Operators](https://www.php.net/manual/en/language.operators.string.php)

[](#string-operators)

Operator NameOperator UseImplementedFunction CallConcatenation`$a . $b`Yes`$a::__concat($b)`Combination Assignment Operators
--------------------------------

[](#combination-assignment-operators)

There are a few assignment operators that are overloaded. These are an operator and an assignment operator combined[4](#user-content-fn-2-d6399168c96e5415e25a1281eb032679). For example, `+=` is an addition assignment operator.

Operator NameOperator UseImplementedFunction CallAddition Assignment`$a += $b`Yes`$a::__assign_add($b)`Subtraction Assignment`$a -= $b`Yes`$a::__assign_sub($b)`Multiplication Assignment`$a *= $b`Yes`$a::__assign_mul($b)`Division Assignment`$a /= $b`Yes`$a::__assign_div($b)`Modulo Assignment`$a %= $b`Yes`$a::__assign_mod($b)`Exponent Assignment`$a **= $b`Yes`$a::__assign_pow($b)`And Assignment`$a &= $b`Yes`$a::__assign_and($b)`Or Assignment`$a |= $b`Yes`$a::__assign_or($b)`Xor Assignment`$a ^= $b`Yes`$a::__assign_xor($b)`Shift left Assignment`$a = $b`Yes`$a::__assign_sr($b)`Coalesce Assignment`$a ??= $b`NoConcat Assignment`$a .= $b`Yes`$a::__assign_concat($b)`Other Opcodes
-------------

[](#other-opcodes)

There are a number of opcodes, more than 200 in total. Many of them don't make sense to overload. The full opcode list can be found in the `php-src` repo [here](https://github.com/php/php-src/blob/master/Zend/zend_vm_opcodes.h#L86).

Footnotes
---------

1. This might be the same as other opcodes, or maybe there is no opcode for it, but I'm not sure. I haven't tested it yet. [↩](#user-content-fnref-1-d6399168c96e5415e25a1281eb032679) [↩2](#user-content-fnref-1-2-d6399168c96e5415e25a1281eb032679) [↩3](#user-content-fnref-1-3-d6399168c96e5415e25a1281eb032679) [↩4](#user-content-fnref-1-4-d6399168c96e5415e25a1281eb032679) [↩5](#user-content-fnref-1-5-d6399168c96e5415e25a1281eb032679) [↩6](#user-content-fnref-1-6-d6399168c96e5415e25a1281eb032679) [↩7](#user-content-fnref-1-7-d6399168c96e5415e25a1281eb032679) [↩8](#user-content-fnref-1-8-d6399168c96e5415e25a1281eb032679)
2. Internally in the PHP engine, the `>` and `>=` operators are just the `
