Next: Binary Assignment Operators, Up: Overloading Operators [Contents]
Syntax | Instruction |
---|---|
$a + $b | ZEND_ADD |
$a - $b | ZEND_SUB |
$a * $b | ZEND_MUL |
$a / $b | ZEND_DIV |
$a % $b | ZEND_MOD |
$a ** $b | ZEND_POW |
$a << $b | ZEND_SL |
$a >> $b | ZEND_SR |
$a . $b | ZEND_CONCAT |
$a | $b | ZEND_BW_OR |
$a & $b | ZEND_BW_AND |
$a ^ $b | ZEND_BW_XOR |
$a === $b | ZEND_IS_IDENTICAL |
$a !== $b | ZEND_IS_NOT_IDENTICAL |
$a == $b | ZEND_IS_EQUAL |
$a != $b | ZEND_IS_NOT_EQUAL |
$a < $b | ZEND_IS_SMALLER |
$a <= $b | ZEND_IS_SMALLER_OR_EQUAL |
$a xor $b | ZEND_BOOL_XOR |
$a <=> $b | ZEND_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; }); }