Download the PHP package sanmai/later without Composer

On this page you can find all versions of the php package sanmai/later. 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?

Informations about the package later

Latest Stable Version CI Coverage Status Type Coverage

This rigorously tested fully-typed library just works. It neither defines nor throws any exceptions.

Install

composer require sanmai/later

The latest version requires PHP 7.4 or greater.

Use

To use this pattern you need a generator function, yielding a single item of type you want to produce lazily. Pass it to later(), a static wrapper returning a Deferred object:

For example:

And then call get() when needed, as many times as needed:

Using a generator instead of a traditional callback comes with a major benefit: any generator is guaranteed by the language to be used exactly once. You can be sure that it won't be called twice.

But that's not all: read on.

No Callbacks Required

Making a closure generator on the spot isn't always convenient. And not to say these closures are much different from all-too-familiar callbacks. Not at all different from the looks of them.

The power of this pattern is in its ability to make use of any function, previously returning a single value, without any need for any additional callbacks or closures.

Consider this diff:

After adding Later\lazy to the mix:

We can see, this simple, single-line, change in the original method freed our program from creating things it may not need, postponing this process until the last moment, while also avoiding any use of callbacks.

Type Transparency

The library is completely typed. PHPStan, Psalm, and Phan are all routinely supported.

To exploit this capability it is recommended to declare a variable holding this object as \Later\Interfaces\Deferred<Type>.

In this example it will be Deferred<DeepThought>:

Following this approach, a static analyzer will be able to understand what is called, and what is returned.

Eager Execution

What if a program calls for Deferred object, but lazy evaluation is not required? For example, because a result is already available being loaded from a cache.

No problem, there's a function for this:

This deferred-but-not-deferred object implements the same interface, and can be used anywhere where a normal Deferred object would go.

Writing Tests

The underlying Deferred object is fairly lax about input types. It will be happy to accept any iterable, not just generators.

This makes it super easy to use in mocks:

Yet for constant and already-known answers best to use a non-deferred variant:

And that's it. No need to go through loops assembling closures and whatnot.

If nothing else, one can make a common mock for it:

API Overview

Method Takes Returns
Later\lazy() iterable<T> \Later\Interfaces\Deferred<T>
Later\later() A generator callback for T \Later\Interfaces\Deferred<T>
Later\now() T \Later\Interfaces\Deferred<T>

All versions of later with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sanmai/later contains the following files

Loading the files please wait ....