3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Monad boxing integer side effects * * @package default * @subpackage default * @author Federico Marani **/ class IntegerBox { /** * undocumented function * * @return void **/ public function __construct($value) { $this->value = $value; $this->isNaN = false; $this->infinite = false; } /** * undocumented function * * @return void **/ public static function identity($unboxedInt) { return new IntegerBox($unboxedInt); } /** * undocumented function * * @return void **/ public static function bind($boxedInt, $function) { $newBox = @$function($boxedInt); if ($newBox->value == null) $newBox->infinite = true; return $newBox; } /** * undocumented function * * @return void **/ public function __toString() { if ($this->isNaN) return "NaN"; if ($this->infinite) return "Infinite"; return strval($this->value); } } // END class IntegerBox $plusOne = function($x) { return IntegerBox::identity($x->value + 1); }; $one = IntegerBox::identity(1); $two = IntegerBox::bind($one, $plusOne); print "$two\n"; $divideByZero = function($x) { return IntegerBox::identity(bcdiv($x->value, 0)); }; $newVal = IntegerBox::bind($one, $divideByZero); print "$newVal\n";
Output for 5.3.0 - 5.5.28, 5.5.31 - 5.5.33, 5.5.35 - 5.6.12, 5.6.16 - 5.6.19, 5.6.21 - 7.0.4, 7.0.6 - 7.1.0
2 Infinite
Output for 7.0.5
2 Fatal error: Uncaught Error: Call to undefined function bcdiv() in /in/A3r6E:68 Stack trace: #0 /in/A3r6E(39): {closure}(Object(IntegerBox)) #1 /in/A3r6E(70): IntegerBox::bind(Object(IntegerBox), Object(Closure)) #2 {main} thrown in /in/A3r6E on line 68
Process exited with code 255.
Output for 5.5.29 - 5.5.30, 5.5.34, 5.6.13 - 5.6.15, 5.6.20
2
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/A3r6E on line 60
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/A3r6E on line 60
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/A3r6E on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/A3r6E on line 16
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/A3r6E on line 16
Process exited with code 255.