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.
bcmath PHP extension is unavailable.bc* functions (e.g., bcadd, bcdiv) without altering existing code, making it a drop-in compatibility layer.bcmath (benchmark-dependent). Critical for high-throughput systems (e.g., bulk crypto ops).require—no configuration or middleware needed.bcmath_compat autoloader to hijack bc* calls (risk: conflicts with other overrides).BCMathCompat::bcadd()) to avoid namespace pollution.extension_loaded('bcmath') and swap implementations dynamically.BCMath) to abstract the backend.bc* calls in Laravel’s Str/Arr helpers for consistency.| Risk Area | Mitigation Strategy |
|---|---|
| Performance Impact | Profile critical paths; cache results for idempotent ops (e.g., bcdiv in rate limits). |
| Precision Errors | Validate inputs/outputs against native bcmath for edge cases (e.g., bcdiv rounding). |
| Namespace Collisions | Use explicit aliases (e.g., \BCMathCompat\bcadd) or a dedicated namespace. |
| Thread Safety | Stateless design; no risk in Laravel’s request-per-thread model. |
| Future-Proofing | Monitor upstream bcmath extension adoption; plan for hybrid mode (fallback + native). |
bc* functions? (e.g., phpseclib, paragonie/random_compat, custom crypto).bcmath? Justify the tradeoff for non-critical paths.bcmath (e.g., bcdiv('1', '3', 10) → 0.3333333333)?bcmath) be enabled as a primary backend with this as a fallback?composer.json under require-dev (if only for testing) or require (production fallback).BCMath facade to a dynamic resolver (checks for native extension first).Mockery or PHPUnit to isolate bc* calls in unit tests.bcmath (no active conflicts).bcdiv results for repeated queries).bc* usage (e.g., grep -r "bc[^m]").bcmath extension.app/Helpers/BCMath.php) to avoid autoloading risks.bc* calls.$this->app->singleton('bcmath', function () {
return extension_loaded('bcmath') ? new NativeBCMath() : new BCMathCompat();
});
| Component | Compatibility Notes |
|---|---|
| Laravel Core | No direct impact; bc* calls must be explicit (e.g., not in framework internals). |
| Third-Party Libraries | Test with phpseclib, paragonie/halite, or web3/php if they use bc*. |
| Custom Code | Replace direct bc* calls with a wrapper class (e.g., app/Utils/Math.php). |
| Database Interactions | Safe for numeric storage (e.g., bcdiv results → DECIMAL columns). |
bc* calls (3–5 days).composer update phpseclib/bcmath_compat).composer.json if stability is critical (e.g., ^0.1).bcscale handling).bcmath adoption in hosting environments to sunset the fallback.bc* usage.bc* calls; compare outputs with native bcmath.bcmath fallback.bcmod performance, bcscale max value).bc* in hot paths (e.g., loops, real-time ops).bcdiv in rate-limited APIs).bc* processing.bcpow).| Scenario | Impact | Mitigation |
|---|---|---|
| Precision Bug | Incorrect financial/crypto ops | Validate against native bcmath. |
| Performance Collapse | Slow API responses | Rate-limit bc* calls; cache. |
| Hosting Extension Added | Unintended fallback usage | Dynamic resolver to prefer native. |
| Package Abandonment | Broken compatibility | Fork or migrate to GMP extension. |
bc* usage.bc* call frequency via Laravel DebugbarHow can I help you explore Laravel packages today?