public static function create($val)
    {
        if (self::$_prefer) {
            switch (self::$_prefer) {
                case 'gmp':
                    $ret = new BigIntegerGmp();
                    break;
                case 'bcmath':
                    $ret = new BigIntegerBcmath();
                    break;
                default:
                    throw new \UnexpectedValueException('Unknown number implementation: ' . self::$_prefer);
            }
        }
        else {
            // autodetect
            if (extension_loaded('gmp')) {
                $ret = new BigIntegerGmp();
            }
            elseif (extension_loaded('bcmath')) {
                $ret = new BigIntegerBcmath();
            }
            else {
                // TODO: potentially offer pure php implementation?
                throw new \RuntimeException('Requires GMP or bcmath extension.');
            }
        }

        if (is_int($val)) {
            $ret->_fromInteger($val);
        }
        else {
            // convert to string, if not already one
            $val = (string)$val;

            // validate string
            if (!preg_match('/^-?[0-9]+$/', $val)) {
                throw new \InvalidArgumentException('Expects a string representation of an integer.');
            }
            $ret->_fromString($val);
        }

        return $ret;
    }
public static function create($val)
    {
        if (self::$_prefer) {
            switch (self::$_prefer) {
                case 'gmp':
                    $ret = new BigIntegerGmp();
                    break;
                case 'bcmath':
                    $ret = new BigIntegerBcmath();
                    break;
                default:
                    throw new \UnexpectedValueException('Unknown number implementation: ' . self::$_prefer);
            }
        }
        else {
            // autodetect
            if (extension_loaded('gmp')) {
                $ret = new BigIntegerGmp();
            }
            elseif (extension_loaded('bcmath')) {
                $ret = new BigIntegerBcmath();
            }
            else {
                // TODO: potentially offer pure php implementation?
                throw new \RuntimeException('Requires GMP or bcmath extension.');
            }
        }

        if (is_int($val)) {
            $ret->_fromInteger($val);
        }
        else {
            // convert to string, if not already one
            $val = (string)$val;

            // validate string
            if (!preg_match('/^-?[0-9]+$/', $val)) {
                throw new \InvalidArgumentException('Expects a string representation of an integer.');
            }
            $ret->_fromString($val);
        }

        return $ret;
    }
public static function create ( $ val ) { if ( self :: $ _prefer ) { switch ( self :: $ _prefer ) { case 'gmp' : $ ret = new BigIntegerGmp ( ) ; break ; case 'bcmath' : $ ret = new BigIntegerBcmath ( ) ; break ; default : throw new \ UnexpectedValueException ( 'Unknown number implementation: ' . self :: $ _prefer ) ; } } else { // autodetect if ( extension_loaded ( 'gmp' ) ) { $ ret = new BigIntegerGmp ( ) ; } elseif ( extension_loaded ( 'bcmath' ) ) { $ ret = new BigIntegerBcmath ( ) ; } else { // TODO: potentially offer pure php implementation? throw new \ RuntimeException ( 'Requires GMP or bcmath extension.' ) ; } } if ( is_int ( $ val ) ) { $ ret -> _fromInteger ( $ val ) ; } else { // convert to string, if not already one $ val = ( string ) $ val ; // validate string if ( ! preg_match ( '/^-?[0-9]+$/' , $ val ) ) { throw new \ InvalidArgumentException ( 'Expects a string representation of an integer.' ) ; } $ ret -> _fromString ( $ val ) ; } return $ ret ; }
Create a BigInteger instance based off the base 10 string or an integer.
@param string|int $val
@return BigInteger
@throws \InvalidArgumentException
Create a BigInteger instance based off the base 10 string or an integer .