3v4l.org

run code in 300+ PHP versions simultaneously
<?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 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Creation of dynamic property IntegerBox::$value is deprecated in /in/A3r6E on line 18 Deprecated: Creation of dynamic property IntegerBox::$isNaN is deprecated in /in/A3r6E on line 19 Deprecated: Creation of dynamic property IntegerBox::$infinite is deprecated in /in/A3r6E on line 20 2 Fatal error: Uncaught DivisionByZeroError: Division by zero in /in/A3r6E:68 Stack trace: #0 /in/A3r6E(68): bcdiv('1', '0') #1 /in/A3r6E(39): {closure}(Object(IntegerBox)) #2 /in/A3r6E(70): IntegerBox::bind(Object(IntegerBox), Object(Closure)) #3 {main} thrown in /in/A3r6E on line 68
Process exited with code 255.
Output for 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28
2 Fatal error: Uncaught DivisionByZeroError: Division by zero in /in/A3r6E:68 Stack trace: #0 /in/A3r6E(68): bcdiv('1', '0') #1 /in/A3r6E(39): {closure}(Object(IntegerBox)) #2 /in/A3r6E(70): IntegerBox::bind(Object(IntegerBox), Object(Closure)) #3 {main} thrown in /in/A3r6E on line 68
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.26, 7.4.33, 8.0.13
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.3.0 - 5.3.29, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.32
2 Infinite
Output for 5.4.0 - 5.4.45
2
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.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.3.11, 4.4.0 - 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.

preferences:
272.7 ms | 401 KiB | 460 Q