Download the PHP package serafim/pipe without Composer
On this page you can find all versions of the php package serafim/pipe. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package pipe
Short Description Library for implementing function call chains
License MIT
Homepage https://github.com/phpfn
Informations about the package pipe
Pipe
Object-oriented pipe operator implementation based on RFC Pipe Operator.
Installation
Library can be installed into any PHP application:
In order to access library make sure to include vendor/autoload.php
in your file.
Usage
A common PHP OOP pattern is the use of method chaining, or what is also known as "Fluent Expressions". So named for the way one method flows into the next to form a conceptual hyper-expression.
However, when using the functional approach this can lead to reduced readability, polluted symbol tables, or static-analysis defying type inconsistency such as in the following example:
The pipe library fixes this problem, allows you to chain the execution of pure functions:
Another Example
See: https://wiki.php.net/rfc/pipe-operator#file_collection_example
With this library, the above could be easily rewritten as:
Working With Value
To pass a value as an argument to a function, use the
underscore (_
) character:
You can omit parentheses if only one argument is used:
To get the value, use one of the options:
Working With Namespace
Let's take a simple example of such code:
Let's try to manage the namespace:
Please note that the use
function applies only to the subsequent function,
all further operations performed in the current context:
In order to perform several operations in another namespace, use an anonymous
function as the second use
argument.
Note that the behavior of the
->use()
method differs depending on whether the second argument is passed.