1212
1313namespace bcmath_compat ;
1414
15+ // For PHP < 7.0, we need to handle class_alias differently
1516// Check phpseclib version and use appropriate namespace
1617if (class_exists ('\phpseclib3\Math\BigInteger ' )) {
1718 // phpseclib 3.x
18- class_alias ('\phpseclib3\Math\BigInteger ' , '\bcmath_compat\BigInteger ' );
19+ if (!class_exists ('\bcmath_compat\BigInteger ' )) {
20+ class_alias ('\phpseclib3\Math\BigInteger ' , '\bcmath_compat\BigInteger ' );
21+ }
1922} elseif (class_exists ('\phpseclib\Math\BigInteger ' )) {
2023 // phpseclib 2.x
21- class_alias ('\phpseclib\Math\BigInteger ' , '\bcmath_compat\BigInteger ' );
24+ if (!class_exists ('\bcmath_compat\BigInteger ' )) {
25+ class_alias ('\phpseclib\Math\BigInteger ' , '\bcmath_compat\BigInteger ' );
26+ }
2227} else {
2328 throw new \RuntimeException ('phpseclib is not installed ' );
2429}
2530
26- use bcmath_compat \BigInteger ;
27-
2831/**
2932 * BCMath Emulation Class
3033 *
@@ -92,7 +95,7 @@ private static function format($x, $scale, $pad)
9295 */
9396 private static function isNegative ($ x )
9497 {
95- return $ x ->compare (new BigInteger ()) < 0 ;
98+ return $ x ->compare (new \ bcmath_compat \ BigInteger ()) < 0 ;
9699 }
97100
98101 /**
@@ -171,7 +174,7 @@ private static function div($x, $y, $scale, $pad)
171174 }
172175
173176 $ temp = '1 ' . str_repeat ('0 ' , $ scale );
174- $ temp = new BigInteger ($ temp );
177+ $ temp = new \ bcmath_compat \ BigInteger ($ temp );
175178 list ($ q ) = $ x ->multiply ($ temp )->divide ($ y );
176179
177180 return self ::format ($ q , $ scale , $ scale );
@@ -217,8 +220,8 @@ private static function mod($x, $y, $scale, $pad)
217220 */
218221 private static function comp ($ x , $ y , $ scale , $ pad )
219222 {
220- $ x = new BigInteger ($ x [0 ] . substr ($ x [1 ], 0 , $ scale ));
221- $ y = new BigInteger ($ y [0 ] . substr ($ y [1 ], 0 , $ scale ));
223+ $ x = new \ bcmath_compat \ BigInteger ($ x [0 ] . substr ($ x [1 ], 0 , $ scale ));
224+ $ y = new \ bcmath_compat \ BigInteger ($ y [0 ] . substr ($ y [1 ], 0 , $ scale ));
222225
223226 return $ x ->compare ($ y );
224227 }
@@ -244,9 +247,9 @@ private static function pow($x, $y, $scale, $pad)
244247 }
245248
246249 $ min = defined ('PHP_INT_MIN ' ) ? PHP_INT_MIN : ~PHP_INT_MAX ;
247- $ y_big = new BigInteger ($ y );
248- $ max_big = new BigInteger (PHP_INT_MAX );
249- $ min_big = new BigInteger ($ min );
250+ $ y_big = new \ bcmath_compat \ BigInteger ($ y );
251+ $ max_big = new \ bcmath_compat \ BigInteger (PHP_INT_MAX );
252+ $ min_big = new \ bcmath_compat \ BigInteger ($ min );
250253 if ($ y_big ->compare ($ max_big ) > 0 || $ y_big ->compare ($ min_big ) <= 0 ) {
251254 if (class_exists ('\ValueError ' )) {
252255 throw new \ValueError ('bcpow(): Argument #2 ($exponent) is too large ' );
@@ -259,15 +262,15 @@ private static function pow($x, $y, $scale, $pad)
259262 $ sign = self ::isNegative ($ x ) ? '- ' : '' ;
260263 $ x = $ x ->abs ();
261264
262- $ r = new BigInteger (1 );
265+ $ r = new \ bcmath_compat \ BigInteger (1 );
263266
264267 for ($ i = 0 ; $ i < abs ($ y ); $ i ++) {
265268 $ r = $ r ->multiply ($ x );
266269 }
267270
268271 if ($ y < 0 ) {
269272 $ temp = '1 ' . str_repeat ('0 ' , $ scale + $ pad * abs ($ y ));
270- $ temp = new BigInteger ($ temp );
273+ $ temp = new \ bcmath_compat \ BigInteger ($ temp );
271274 list ($ r ) = $ temp ->divide ($ r );
272275 $ pad = $ scale ;
273276 } else {
@@ -306,9 +309,9 @@ private static function powmod($x, $e, $n, $scale, $pad)
306309 '1 ' ;
307310 }
308311
309- $ x = new BigInteger ($ x );
310- $ e = new BigInteger ($ e );
311- $ n = new BigInteger ($ n );
312+ $ x = new \ bcmath_compat \ BigInteger ($ x );
313+ $ e = new \ bcmath_compat \ BigInteger ($ e );
314+ $ n = new \ bcmath_compat \ BigInteger ($ n );
312315
313316 $ z = $ x ->powMod ($ e , $ n );
314317
@@ -532,7 +535,7 @@ public static function __callStatic($name, $arguments)
532535 $ num [1 ] = '' ;
533536 }
534537 $ num [1 ] = str_pad ($ num [1 ], $ pad , '0 ' );
535- $ num = new BigInteger ($ num [0 ] . $ num [1 ]);
538+ $ num = new \ bcmath_compat \ BigInteger ($ num [0 ] . $ num [1 ]);
536539 }
537540 break ;
538541 case 'comp ' :
0 commit comments