PHPackages                             spatie/or-else - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/or-else

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/or-else
==============

A trait to add orElse functions to a class

1.0.0(10y ago)3414441MITPHPPHP &gt;=5.4.0

Since Jun 12Pushed 3mo ago5 watchersCompare

[ Source](https://github.com/spatie/or-else)[ Packagist](https://packagist.org/packages/spatie/or-else)[ Docs](https://github.com/spatie/or-else)[ RSS](/packages/spatie-or-else/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (1)

or-else
=======

[](#or-else)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9068fd27213609e217dfcdd231274065d519ba5c1ca095e05dd1c6057c954347/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6f722d656c73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/or-else)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d41bbd9739f22f39f23838d1ca2e7b3e20d901a9b8f31589dbbb43f85cd374c0/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6f722d656c73652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/or-else)[![Quality Score](https://camo.githubusercontent.com/9d6ea1bf8c8f1f82b7a03e630ecbfd3086f9108446865849f16053bcc0ae03a4/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6f722d656c73652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/or-else)[![Total Downloads](https://camo.githubusercontent.com/fd2c8eb68658d84153d34289338088795378cb839432a82bd84e2b2f8364a3ae/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6f722d656c73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/or-else)

This package adds an `orElse`-trait to your project.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/0f596c2ab3d5443413ecff149bd591bbe96177a1c449cc746754236f7b7492e7/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6f722d656c73652e6a70673f743d31)](https://spatie.be/github-ad-click/or-else)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Install
-------

[](#install)

You can install the package via composer:

```
$ composer require spatie/or-else
```

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Usage
-----

[](#usage)

When implementing the `OrElse`-trait to a class, all methods of the class will have a `OrElse`-variant. That variant has an extra parameter that will be returned if the original function returns `null` or `false`.

Consider this simple class that implements the `orElse`-trait.

```
use Spatie\OrElse\OrElse;

class TestClass {

    use OrElse;

    /**
     * This function will return the given argument.
     *
     * @return string
     */
    public function willReturn($value)
    {
       return $value;
    }

}
```

The trait dynamically adds a `willReturnOrElse`-method.

```
$testClass = new TestClass;
$testClass->willReturn('value'); // returns 'value';
$testClass->willReturnOrElse('value', 'otherValue'); // returns 'value';
$testClass->willReturnOrElse(null, 'otherValue'); // returns 'otherValue';
$testClass->willReturnOrElse(false, 'otherValue'); // returns 'otherValue';
$testClass->willReturnOrElse(false, function() { return 'closureValue'; }); // returns 'closureValue';
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://murze.be)
- [Edd Mann](https://twitter.com/edd_mann)
- [All Contributors](../../contributors)

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance54

Moderate activity, may be stable

Popularity23

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 65.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

3988d ago

Major Versions

0.0.1 → 1.0.02015-06-17

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (19 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![danhunsaker](https://avatars.githubusercontent.com/u/1534396?v=4)](https://github.com/danhunsaker "danhunsaker (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

leagueor-else

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-or-else/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-or-else/health.svg)](https://phpackages.com/packages/spatie-or-else)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
