- What is michaelcozzolino/php-redefinitions?
- It is a Composer-ready PHP library that lets you redefine existing native PHP functions and methods at runtime. It is aimed at monkey-patching legacy code, stubbing third-party functions, and swapping implementations in tests without editing the original source. It is framework-agnostic, so it can be used in plain PHP projects or Laravel applications.
- How do I install it in a Laravel project?
- Install it with Composer using composer require michaelcozzolino/php-redefinitions. Because the package is framework-agnostic, no Laravel service provider or extra configuration is described in the provided context. After installation, include Composer's autoloader and apply the redefinitions where your code or tests need them.
- Does this package work with Laravel?
- Yes. The package is framework-agnostic and Composer-ready, so it can be added to a Laravel application like any other Composer dependency. It is particularly useful in Laravel tests when you need to stub native functions or legacy code that is hard to inject. No Laravel-specific integration is mentioned in the available package information.
- Can it mock native PHP functions like time or rand?
- Yes, redefining native PHP functions is the core purpose of the package. You can swap implementations at runtime so calls to functions such as time or random generators can return controlled values in tests. Check the package documentation for the exact API and any restrictions on specific internal functions.
- Can I redefine methods on existing classes?
- The package is described as supporting redefinitions of existing functions and methods at runtime. That makes it suitable for monkey-patching legacy class methods or third-party code when dependency injection is not practical. As with any runtime override, keep the scope narrow and verify behavior with tests.
- Does it edit the original source code?
- No. The stated purpose is runtime redefinition, so you do not need to edit the original source files to swap implementations. This helps keep vendor code and legacy files untouched while still allowing controlled overrides. Review how the redefinitions are loaded so the override applies only where intended.
- Is it safe to use in production?
- The package is primarily suited to tests, legacy shims, and controlled runtime swaps rather than broad production monkey-patching. Changing native functions or methods can make behavior harder to trace, so use it deliberately and document any overrides. If you use it in production, test thoroughly and keep the scope as small as possible.
- What are alternatives for mocking native PHP functions?
- Common alternatives include the uopz PHP extension, php-mock-style libraries for native function mocking, or refactoring code to use dependency injection and test doubles. This package's advantage is that it is framework-agnostic and Composer-ready for runtime redefinitions. Choose based on whether you can install PHP extensions and how much legacy code you need to isolate.
- Which PHP versions are supported?
- The provided context does not list specific PHP version constraints. Check the package's composer.json and Packagist metadata for the required PHP version before installing it in a Laravel project. Laravel version compatibility will also depend on the PHP versions that your Laravel release supports.
- Is the package actively maintained?
- The repository is listed as unknown in the provided context, so maintenance activity cannot be confirmed from this information alone. Check Packagist and the package's source repository for recent releases, open issues, and compatibility updates. For production or long-term use, pin a version and review the code or test coverage before relying on it.