Passing variables to a PHP extension by reference
Any variable can be passed to a PHP extension by reference. This differs from Java™ where scalar types and object references are always passed by value. This tutorial extends the big decimal extension by adding a new extension function that negates the value passed. The negated value is passed by reference.
1 Add the following Java code to the BigDecimalExtension class:
@XAPIArguments(MandatoryArguments = 1, PassSemantics = { XAPIPassSemantics.ByReference })
@XAPIFunction("negate_bigdecimal")
public void negate(RuntimeContext ctxt) {
BigDecimal arg = new BigDecimal(ctxt.getStringArgument(0).getString());
BigDecimal result = arg.negate();
ctxt.setArgument(0, result.toString());
}
2 Change the sample.php code to exercise the new function.
<?php $value = '111111111111111'; negate_bigdecimal($value); echo "Negated big decimal ".$value; ?>
3 Run the IBM® WebSphere® sMash application and point a Web browser at http://localhost:8080/sample.php
The example uses the _XAPIPassSemantics_ annotation, this tells the PHP runtime to pass the variable by reference instead of by value (which is the default).