Download the PHP package sebastiaanluca/php-pipe-operator without Composer

On this page you can find all versions of the php package sebastiaanluca/php-pipe-operator. 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 php-pipe-operator

PHP Pipe Operator

Latest stable release Build status Total downloads Total stars

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter

Method chaining (or fluent expressions) for any value using any method.

Table of contents

Requirements

How to install

Via Composer:

How to use

The basics

The basic gist of the package is that it takes a value and performs one or more actions on it. A simple example:

A few alternatives to create the same instance:

Of course that's not very useful since you could've just used strtoupper('hello') and be done with it, but the goal is to make multi-method calls on a value easier to read and write:

Note that in comparison to the original RFC, there's no need to pass the initial value to methods that receive the value as first parameter and have no other required parameters. The previous value is always passed as first parameter. In effect, both of the following examples will work:

In contrast, if a method takes e.g. a setting before the previous value, we need to set it manually using the replacement identifier (the globally available PIPED_VALUE constant). This identifier can be placed anywhere in the method call, it will simply be replaced by the previous value.

Using first class callable syntax (enabling IDE autocompletion)

Since PHP 8.1, you can use a first class callable syntax, or simply put an anonymous function, to pipe the value through. This enables full method autocompletion.

Or using parameters:

Using closures

Sometimes standard methods don't cut it and you need to perform a custom operation on a value in the process. You can do so using a closure:

Using class methods

The same is possible using a class method (regardless of visibility):

Class method alternatives

If you don't want to use the internal pipe proxy and pass $this, there are two other ways you can use class methods.

Using first class callable syntax:

Using an array (for public methods only):

By parsing the callable method to a closure:

What does it solve?

This package is based on the pipe operator RFC by Sara Golemon (2016), who explains the problem as:

A common PHP OOP pattern is the use of method chaining, or what is also known as “Fluent Expressions”. […] This works well enough for OOP classes which were designed for fluent calling, however it is impossible, or at least unnecessarily arduous, to adapt non-fluent classes to this usage style, harder still for functional interfaces.

Coming across the proposal, I also blogged about it.

A simple example

Say you want to get the subdomain from a URL, you end up with something like this:

This works, of course, but it's quite verbose and repetitive.

Another way of writing

Same result, different style:

This might be the worst of all solutions, as it requires you to start reading from the center, work your way towards the outer methods, and keep switching back and forth. The more methods and variants, the more difficult to get a sense of what's going on.

More examples of the issue at hand

See Sara's RFC for more complex and real-world examples.

Notes

While this packages makes a good attempt at bringing the pipe operator to PHP, it unfortunately does not offer autocompletion on chained methods. For that to work we need the real deal, so make some noise and get the people in charge to vote for Sara's RFC!

License

This package operates under the MIT License (MIT). Please see LICENSE for more information.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

Contributing

Please see CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About

My name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project.

Have a project that could use some guidance? Send me an e-mail at [email protected]!


All versions of php-pipe-operator with dependencies

PHP Build Version
Package Version
Requires php Version ~8.1|~8.2
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 sebastiaanluca/php-pipe-operator contains the following files

Loading the files please wait ....