Awesome Open Source
Awesome Open Source


Object-oriented pipe operator implementation based on RFC Pipe Operator.


Library can be installed into any PHP application:

$ composer require phpfn/pipe

In order to access library make sure to include vendor/autoload.php in your file.


require __DIR__ . '/vendor/autoload.php';


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:


$snakeCase = strtolower(
    preg_replace('/(.)(?=[A-Z])/u', '$1_', 
        preg_replace('/\s+/u', '', 
var_dump($snakeCase); // "hello_world"

The pipe library fixes this problem, allows you to chain the execution of pure functions:

pipe('Hello World')
    ->preg_replace('/\s+/u', '', _)
    ->preg_replace('/(.)(?=[A-Z])/u', '$1_', _)
// string(11) "hello_world"

Another Example


$result = array_merge(
            function ($x) use ($arg) {
                return $arg . '/' . $x;
                function ($x) {
                    return $x !== '.' && $x !== '..';

With this library, the above could be easily rewritten as:


$result = pipe($arg)
    ->array_filter(_, fn($x): bool => $x !== '.' && $x !== '..')
    ->array_map(fn($x): string => $arg . '/' . $x, _)
    ->array_merge($result, _);

Working With Value

To pass a value as an argument to a function, use the underscore (_) character:


    ->str_replace('o', '', _)
    ->var_dump; // "hell"

You can omit parentheses if only one argument is used:


    ->var_dump; // bool(false) 

To get the value, use one of the options:

$context = pipe('hello')->strtoupper;

// object(Fun\Pipe\Pipe)#8 (1) { ... } 

// string(5) "HELLO"

Working With Namespace

Let's take a simple example of such code:

namespace {
    function foo() { return __FUNCTION__; }

namespace Example {
    function foo() { return __FUNCTION__; }

Let's try to manage the namespace:

$context = pipe()->use('Example')->foo;

echo $context(); // 'Example\\foo'

$context = $context->foo;

echo $context(); // 'foo'

Please note that the use function applies only to the subsequent function, all further operations performed in the current context:

    ->use('Some\\Namespace')->foo // Call "\Some\Namespace\foo()"
    ->foo // Call "\foo()"

In order to perform several operations in another namespace, use an anonymous function as the second use argument.

    ->use('Some\\Namespace', fn($pipe) => 
            ->a // Call "\Some\Namespace\a()"
            ->b // Call "\Some\Namespace\b()"
    ->a // Call "a()"

Note that the behavior of the ->use() method differs depending on whether the second argument is passed.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
php (16,599
functional (148
pipe (44
chain (26