phpseclib/bcmath_compat
BCMath compatibility layer for phpseclib, providing pure-PHP replacements for bcmath functions when the ext-bcmath extension isn’t available. Helps ensure consistent big integer math support across environments for cryptography and related operations.
Install via Composer:
composer require phpseclib/bcmath_compat
Add to composer.json under require or require-dev if only needed for testing.
Autoloading:
The package auto-registers when installed. No manual require or service provider setup is needed.
First Use Case:
Replace native bcmath calls in a script or library:
// Before (requires bcmath extension)
$result = bcadd('1.23456789', '9.87654321', 10);
// After (works with or without bcmath)
$result = bcadd('1.23456789', '9.87654321', 10);
Test in a shared hosting environment where bcmath is disabled.
Fallback Strategy:
bcmath_compat as a drop-in replacement for bcmath in libraries or applications.bc* functions in a helper to auto-fallback:
if (!extension_loaded('bcmath')) {
require 'vendor/phpseclib/bcmath_compat/bcmath.php';
}
Cryptography Use Cases:
$modulus = bcpowmod('2', '1024', '12345678901234567890'); // Modular exponentiation
Precision Control:
$total = bcadd('1.1', '2.2', 2); // Result: 3.30 (not 3.3)
Testing:
bcmath in unit tests by requiring the compat layer:
// In PHPUnit setup
require 'vendor/phpseclib/bcmath_compat/bcmath.php';
Laravel-Specific:
AppServiceProvider to ensure bcmath functions are available globally:
public function boot() {
if (!extension_loaded('bcmath')) {
require app_path('vendor/phpseclib/bcmath_compat/bcmath.php');
}
}
if (!app()->bound('bcmath_loaded')) {
app()->singleton('bcmath_loaded', function () {
if (!extension_loaded('bcmath')) {
require app_path('vendor/phpseclib/bcmath_compat/bcmath.php');
}
return true;
});
}
Performance Note:
bcmath. Benchmark critical paths if performance is a concern.Precision Limits:
bcmath. Test edge cases (e.g., bcdiv('1', '3', 100)).Floating-Point Behavior:
bcmath due to underlying PHP gmp/bcmath emulation. Validate outputs in financial or scientific apps.Thread Safety:
bcmath-dependent libraries if loaded multiple times. Use a single require per app.Error Handling:
bcmath throws warnings for invalid operations (e.g., bcdiv('1', '0')). The compat layer may return false or null instead. Normalize errors:
$result = bcdiv($a, $b, $scale);
if ($result === false) {
throw new \RuntimeException("Division by zero or invalid scale");
}
Verify Compatibility:
if (!function_exists('bcadd')) {
require 'vendor/phpseclib/bcmath_compat/bcmath.php';
}
bcmath (if available) for critical paths.Performance Profiling:
bcmath vs. compat layer in production-like environments.Custom Functions:
bc* functions to the included bcmath.php file (e.g., bcpow):
if (!function_exists('bcpow')) {
function bcpow($x, $y, $scale = 0) {
return bcpowmod($x, $y, '1', $scale);
}
}
Configuration:
bcmath.php file before requiring it:
define('BCMATH_SCALE', 20); // Set global scale
require 'vendor/phpseclib/bcmath_compat/bcmath.php';
Fallback Logic:
trait BcMathFallback {
public function bcadd($x, $y, $scale = 0) {
return extension_loaded('bcmath') ? bcadd($x, $y, $scale) : \bcadd($x, $y, $scale);
}
}
How can I help you explore Laravel packages today?