3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Immutable class for handling numbers with precision. * * Never change the $number or the $precision after the creation of the object. */ class DecimalNumber { private $precision; private $number; /** * Get an instance given a string and a precision * @param string $string The decimal number in string form * @param int $precision The precision of the number * @return DecimalNumber Return new instance */ public static function createWithString($string, $precision = 4) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); return $number; } /** * Perform an addition operation * @param DecimalNumber $number the number to add * @return DecimalNumber Return the sum */ public function add(DecimalNumber $number) { return DecimalNumber::decimalNumberWithString(bcadd($this->number, $number, $this->precision)); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $d = DecimalNumber::createWithString('.7'); echo $d->add(DecimalNumber::createWithString('.1'));
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to undefined method DecimalNumber::decimalNumberWithString() in /in/gR03N:35 Stack trace: #0 /in/gR03N(49): DecimalNumber->add(Object(DecimalNumber)) #1 {main} thrown in /in/gR03N on line 35
Process exited with code 255.
Output for 5.3.0 - 5.6.28
Fatal error: Call to undefined method DecimalNumber::decimalNumberWithString() in /in/gR03N on line 35
Process exited with code 255.
Output for 5.0.0 - 5.2.17
Fatal error: Call to undefined function get_called_class() in /in/gR03N on line 20
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/gR03N on line 10
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/gR03N on line 10
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/gR03N on line 10
Process exited with code 255.