spatie/or-else
Adds an OrElse trait that auto-creates “*OrElse” method variants for your class methods. These variants accept a fallback value that’s returned whenever the original method returns null or false, helping you avoid repetitive default-handling code.
Architecture Fit
orElse trait (similar to PHP’s native orElse behavior in collections or nullable objects), but Laravel already includes robust alternatives (e.g., Collection::when(), tap(), defaultIfEmpty(), or null coalescing ??). The package’s core functionality is redundant for most Laravel applications, which leverage PHP 7.0+ features (e.g., null coalescing, optional chaining) and Laravel’s built-in helpers.Integration Feasibility
composer require spatie/or-else), and the trait can be applied to any class. However, the package’s abandoned status (last release in 2015) raises concerns about compatibility with modern PHP/Laravel versions.Technical Risk
create_function, dynamic function calls) or lack of type safety.// Instead of:
$value = $object->orElse(fn() => $default);
// Use:
$value = $object->value ?? $default; // PHP 7.0+
$value = $collection->first(fn($item) => $item->active) ?? $default;
Key Questions
optional() (PHP 7.1+), tap(), or when() replace this functionality with zero dependencies?Stack Fit
orElse to custom objects/collections in pre-2015 stacks.Migration Path
orElse calls. If none exist, deprioritize this package.??, optional()).use Spatie\OrElse\OrElse; with:
// For nullable objects:
$value = $object->property ?? $default;
// For collections:
$value = $collection->first(fn($item) => $item->condition) ?? $default;
Compatibility
create_function().Collection macro system.HttpFoundation) that expect modern PHP.Sequencing
orElse calls with modern equivalents.Maintenance
Support
Scaling
Failure Modes
create_function() errors in PHP 7.2+.Ramp-Up
?? or optional()).Final Recommendation:
Do not use for new projects or PHP 7.0+. For legacy PHP < 7.0 systems, replace with native ?? or migrate to a supported stack. The package’s abandoned state and redundant functionality outweigh any benefits.
How can I help you explore Laravel packages today?