datahihi1 / pipefn
Polyfill for PHP 8.5 pipe operator supporting closures, functions, and methods.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/datahihi1/pipefn
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^9 || ^10 || ^11 || ^12
This package is auto-updated.
Last update: 2025-10-28 15:40:34 UTC
README
A lightweight polyfill that provides a pipe() function for method chaining and functional pipelines in PHP 5.6+ applications. This function allows you to chain method calls, closures, functions, and more in a clean, readable way.
Installation
Via Composer
composer require datahihi1/pipefn:dev-main
Manual Installation
- Download the pipefn.phpfile
- Include it in your project:
require_once 'pipefn.php'; 
Usage
The pipe() function takes an initial value and chains it through multiple steps (functions, closures, methods, callables, etc).
Basic Syntax
$result = pipe($value, ...$steps);
Parameters
- $value- The initial value to be piped through the steps
- ...$steps- Variable number of steps, which can be:- String function/method names (global, static, or object methods)
- Arrays in format [callable, [arguments]]or[methodName, [arguments]]
- Closures/functions
- Callable arrays (e.g. [$object, 'method'])
 
Supported Usages
1. Chain closures/functions
pipe(" hi ", fn($v) => trim($v), fn($v) => strtoupper($v), fn($v) => $v . "!"); // Result: "HI!"
2. Global function names
pipe(" test ", 'trim', 'strtoupper'); // Result: "TEST"
3. Static method (Class::method)
pipe("hey", 'MyClass::upper'); pipe("a", ['MyClass::append', ['b']]); // Result: "HEY", "ab"
4. Object method (with or without arguments)
$user = new class('john') { public $name; public function __construct($n){$this->name = $n;} public function setName($n){$this->name=$n; return $this;} public function upper(){ $this->name = strtoupper($this->name); return $this; } public function get(){return $this->name;} }; pipe($user, ['setName', ['jane']], 'upper', 'get'); // Result: "JANE"
5. Callable array (e.g. [[$obj, 'method'], [args]])
pipe("x", [[$obj, 'method'], ['y']]); // Calls $obj->method("x", "y")
6. Closure with multiple arguments
pipe("a", [fn($v, $x, $y) => $v . $x . $y, ['b', 'c']]); // Result: "abc"
7. Function with callback (e.g. array_map)
pipe([1,2,3], ['array_map', [fn($x)=>$x*2]], 'array_sum'); // Result: 12
8. No steps (returns value unchanged)
pipe("hello"); // Result: "hello"
9. Error handling
If a step is not callable or invalid, an InvalidArgumentException is thrown with a clear message.
Requirements
- PHP >= 5.6
- No additional dependencies
Features
- ✅ PHP 5.6+ compatibility
- ✅ Method chaining with arguments
- ✅ Closure/function/callable support
- ✅ Callable array (e.g. [$obj, 'method'])
- ✅ Lightweight (single file)
- ✅ No external dependencies
- ✅ Automatic function/method existence check
- ✅ Clear error messages for debugging
License
This project is open source and available under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.