Download the PHP package phpoption/phpoption without Composer

On this page you can find all versions of the php package phpoption/phpoption. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

All versions of phpoption with dependencies

Informations about the package phpoption

PHP Option Type

This package implements the Option type for PHP!

Banner

Total Downloads Latest Version

Motivation

The Option type is intended for cases where you sometimes might return a value (typically an object), and sometimes you might return a base value (typically null) depending on arguments, or other runtime factors.

Often times, you forget to handle the case where a base value should be returned. Not intentionally of course, but maybe you did not account for all possible states of the system; or maybe you indeed covered all cases, then time goes on, code is refactored, some of these your checks might become invalid, or incomplete. Suddenly, without noticing, the base value case is not handled anymore. As a result, you might sometimes get fatal PHP errors telling you that you called a method on a non-object; users might see blank pages, or worse.

On one hand, the Option type forces a developer to consciously think about both cases (returning a value, or returning a base value). That in itself will already make your code more robust. On the other hand, the Option type also allows the API developer to provide more concise API methods, and empowers the API user in how he consumes these methods.

Installation

Installation is super-easy via Composer:

or add it by hand to your composer.json file.

Usage

Using the Option Type in your API

If you are consuming an existing library, you can also use a shorter version which by default treats null as None, and everything else as Some case:

Case 1: You always Require an Entity in Calling Code

Case 2: Fallback to Default Value If Not Available

More Examples

No More Boiler Plate Code

No More Control Flow Exceptions

More Concise Null Handling

Trying Multiple Alternative Options

If you'd like to try multiple alternatives, the orElse method allows you to do this very elegantly:

The first option which is non-empty will be returned. This is especially useful with lazy-evaluated options, see below.

Lazy-Evaluated Options

The above example has the flaw that we would need to evaluate all options when the method is called which creates unnecessary overhead if the first option is already non-empty.

Fortunately, we can easily solve this by using the LazyOption class:

This way, only the options that are necessary will actually be evaluated.

Performance Considerations

Of course, performance is important. Attached is a performance benchmark which you can run on a machine of your choosing.

The overhead incurred by the Option type comes down to the time that it takes to create one object, our wrapper. Also, we need to perform one additional method call to retrieve the value from the wrapper.

The benchmark was run under Ubuntu precise with PHP 5.4.6. As you can see the overhead is surprisingly low, almost negligible.

So in conclusion, unless you plan to call a method thousands of times during a request, there is no reason to stick to the object|null return value; better give your code some options!

Security

If you discover a security vulnerability within this package, please send an email to [email protected] All security vulnerabilities will be promptly addressed. You may view our full security policy here.

License

PHP Option Type is licensed under Apache License 2.0.

For Enterprise

Available as part of the Tidelift Subscription

The maintainers of phpoption/phpoption and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.


Version
Requires php Version ^7.0 || ^8.0

The package phpoption/phpoption contains the following files

Loading the files please wait ....