Next: , Up: Overloading Operators   [Contents]


4.1 Binary Operators

SyntaxInstruction
$a + $bZEND_ADD
$a - $bZEND_SUB
$a * $bZEND_MUL
$a / $bZEND_DIV
$a % $bZEND_MOD
$a ** $bZEND_POW
$a << $bZEND_SL
$a >> $bZEND_SR
$a . $bZEND_CONCAT
$a | $bZEND_BW_OR
$a & $bZEND_BW_AND
$a ^ $bZEND_BW_XOR
$a === $bZEND_IS_IDENTICAL
$a !== $bZEND_IS_NOT_IDENTICAL
$a == $bZEND_IS_EQUAL
$a != $bZEND_IS_NOT_EQUAL
$a < $bZEND_IS_SMALLER
$a <= $bZEND_IS_SMALLER_OR_EQUAL
$a xor $bZEND_BOOL_XOR
$a <=> $bZEND_SPACESHIP

A binary operator takes two operands, and always returns a value. Modification of either operand is allowed, provided that the operand type is IS_CV.

Note that there is no ZEND_IS_GREATER or ZEND_IS_GREATER_OR_EQUAL operator as said in #2.2. Although the PECL operator extension does some hack with extended_value of zend_op to distinguish whether the opcode is compiled from < or >, it requires patching the PHP source code and may break future compatibility.

The recommended alternative solution is shown below.

int
is_smaller_handler(
    zend_execute_data *execute_data
) {
    return op_handler(execute_data, [] (auto zv1, auto zv2, auto rv) {
        if (Z_TYPE_P(zv1) == IS_OBJECT) {
            if (__zobj_has_method(Z_OBJ_P(zv1), "__is_smaller")) {
                // Call `$zv1->__is_smaller($zv2)`.
                return true;
            }
        } else if (Z_TYPE_P(zv2) == IS_OBJECT) {
            if (__zobj_has_method(Z_OBJ_P(zv2), "__is_greater")) {
                // Call `$zv2->__is_greater($zv1)`.
                return true;
            }
        }
        return false;
    });
}