Up: Notes   [Contents]


5.1 Constant Folding

Try compiling the following PHP script:

$a = 2 + 3 * (7 + 9);
$b = 'foo' . 'bar';

You will get:

ASSIGN $a, 50
ASSIGN $b, "foobar"

You can see that, the value of $a and $b is calculated at compile time, and there’s neither arithmetic operations nor string concatenation when the script is running.

The PHP compiler does some optimizations (known as constant folding) by recognizing constant expressions and evaluate them with function zend_const_expr_to_zval, defined in zend_compile.c. Opcode handlers are fetched with functions like get_binary_op, get_unary_op() that hard-code the built-in handler functions.

Therefore, if you overload these operators in your extension, the custom handlers won’t be invoked.